Monday, May 30, 2011

Android EMI Calculator





Another basic application with learning objectives as follows

1. Implementing Reset


Intent refresh = new Intent(this, EMICalculator.class);
startActivity(refresh);
this.finish();

Reset basically means resetting all the fields and getting them back to their original state. In this particular code I am implementing reset by closing the current activity and starting it as a fresh activity again.

2. Implementing Menus

Menus can be implemented by adding and xml file in the and inflating it in an activity, we will see this method in some later application. In this application menus are inflated through the code itself.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add("About").setIcon(R.drawable.about);
menu.add("Exit").setIcon(R.drawable.exit);
return super.onCreateOptionsMenu(menu);
}

3. Handling Menu click

Menu clicks are handelled in a method called onOptionsItemSelected
if(item.getTitle().equals("About")&&item.getTitle()=="About")
{
Intent i=new Intent(this,About.class);
this.finish();
startActivityForResult(i,0);
}

In this particular piece of code ABOUT menu is clicked, by using equals method its verified that which item is clicked then the a fresh activity is started using an intent. Remember when ever you start a new activity always close the current one. Android apps have to run on phone with limited resources, so an activity should not be left open running behind the scene as it will consume valuable resources of the phone.

4. Implementing Image button

imagebutton id="@+id/home" src="@drawable/home" layout_width="wrap_content" layout_height="wrap_content" clickable="true" onclick="myClickHandler"

One more thing that is used in this code although not from android but java is good to mention. I have used number formatter in order to display the results in a desired format.

NumberFormat formatter = new DecimalFormat("#0.00#");

Monday, May 9, 2011

Android Temperature Converter App



A basic android app with an objective of learning how to use

1. Textbox
2. RadioGroup
3. Button


Convert.java
package and.temperature;

import android.app.Activity;


import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

public class Convert extends Activity {

private EditText textInput;
private TextView textOutput;
private RadioGroup rdGroup;
private Float temp=0f;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
textInput=(EditText)findViewById(R.id.textTemp);
textOutput=(TextView)findViewById(R.id.textOutput);
rdGroup=(RadioGroup)findViewById(R.id.radioGroup);
}

public void myClickHandler(View view){
switch(view.getId()){
case R.id.Button01:

try{
temp=new Float(textInput.getText().toString());

switch(rdGroup.getCheckedRadioButtonId())
{
case R.id.radioCelcius:
textOutput.setText(Float.toString(FarenhiteToCelcius(temp))+" C");
break;

case R.id.radioFarenhite:
textOutput.setText(Float.toString(CelciusToFarenhite(temp))+" F");
break;
}
}catch(Exception e)
{
Toast.makeText(this, "Invalid Input",
Toast.LENGTH_LONG).show();
}

break;

}
}

public float FarenhiteToCelcius(float temp){
return ((temp - 32) * 5 / 9);
}

public float CelciusToFarenhite(float temp){
return ((temp * 9) / 5) + 32;

}

public void Close(View view){

// Bundle bd = i.getExtras();
// if(bd.getInt("Close") == 1);

finish();
Log.v("DISPLAY", "closing convert");
}
}