Saturday, June 11, 2011

Android Dynamic Layout







Android has given wonderful method of developing xml based user interface, which at times can be as easy as drag and drop but in case where the developer is not aware of any particular element's requirement in advance layout must be developed on the fly dynamically. For example let us assume that the number of EditTexts required in the layout is not known in advance and that will be decide by the user then that has to be created dynamically.

With this small tutorial the learning objectives can be as follows

1. Declaring and using the linear layout and other components through the code.

LinearLayout linearLayout = new LinearLayout(this);
In this project we are not using xml based UI. Se we have to declare the linear layout in the manner depicted above and then setting the relevant parameters as given in the code attached with this post.



2. Generating multiple EditTexts dynamically.

final EditText editTextArray[] = new EditText[4];

Declaring and array of EditTexts of size 4 because we want to generate 4 EditTexts. We are using a fixed array but this can be replaced by dynamic array as well. When generating any component dynamically its id will not be seen in R.java so please don't worry about that.

for (int i = 0; i < editTextArray.length; i++) {
textView1=new TextView(this);
textView1.setText(i+1+".");
editTextArray[i] = new EditText(this);
editTextArray[i].setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);
editTextArray[i].setWidth(150);
editTextArray[i].setId(i);
tableRow1.addView(textView1);
tableRow1.addView(editTextArray[i]);

}

This particular piece of code generates EditTexts dynamically, it also sets the input type as number decimal and then adding them to a particular row.

3. Generating TextView dynamically and setting their properties such has text size, color etc.

textView5.setTextSize(50.0f);
textView5.setTextColor(Color.RED);

Text view's all the properties can be set through the code itself as it can be seen the size of texts and the color of text is set through the code. For setting color two ways are available one is passing the int id of the color and the second method is using the in build color class.

4. Extracting the values from dynamically generated EditTexts.

for (int i = 0; i < editTextArray.length; i++){
if(editTextArray[i].getText().toString().equals(""))
{
// i am doing nothing in case of blank input
}else
{
sum+=Float.parseFloat(editTextArray[i].getText().toString());
}
}


This particular piece of code is responsible for extracting the data from the dynamically generated EditTexts. I am using equals method to check whether the EditText is blank or not in case of blank i am doing nothing you can give a message to the user that EditText can not be blank or whatever you feel is better.

5. Setting the number type decimal through the code.

editTextArray[i].setInputType(InputType.TYPE_CLASS_NUMBER|InputType.TYPE_NUMBER_FLAG_DECIMAL);

This is how input type can be set from the code itself, in this case we are setting it as a number decimal, it can be set to anything eg. number, phone, decimal etc.

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");
}
}