Monday, February 8, 2010

Creating custom dialog in Android

This blog shows you an example on how to create custom dialog. For more information on creating different kind of dialogs, please visit http://developer.android.com/guide/topics/ui/dialogs.html
1. Create layout for the dialog
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="10sp">
<EditText android:text=""
android:id="@+id/categoryEditText"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:singleLine="true">
</EditText>
</LinearLayout>



2. Override onCreateDialog in the Activity
@Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case CATEGORY_DETAIL:
LayoutInflater li = LayoutInflater.from(this);
View categoryDetailView = li.inflate(R.layout.category_detail, null);

AlertDialog.Builder categoryDetailBuilder = new AlertDialog.Builder(this);
categoryDetailBuilder.setTitle("Edit Category");
categoryDetailBuilder.setView(categoryDetailView);
AlertDialog categoryDetail = categoryDetailBuilder.create();

categoryDetail.setButton("OK", new OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {
AlertDialog categoryDetail = (AlertDialog)dialog;
EditText et = (EditText)categoryDetail.findViewById(R.id.categoryEditText);
if (categories.get(selectedIndex)!=null){
//... some code
}
});

categoryDetail.setButton2("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
return;
}});

return categoryDetail;
default:
break;
}
return null;
}


3. Override onPrepareDialog to dynamically set default value before the dialog is open
@Override
protected void onPrepareDialog(int id, Dialog dialog) {
switch (id) {
case CATEGORY_DETAIL:
AlertDialog categoryDetail = (AlertDialog)dialog;
EditText et = (EditText)categoryDetail.findViewById(R.id.categoryEditText);
et.setText(defaultValue);
break;
default:
break;
}
}


4. Call showDialog to display the dialog
static final private int CATEGORY_DETAIL = 1;
//... some code
showDialog(CATEGORY_DETAIL);


Monday, February 1, 2010

Passing a list of objects between Activities

In Android, you can use Bundle to pass data between activities. There are many functions to pass Integer, String, etc. To pass an object, you will need to implement Parcelable interface and override some functions to serialize/de-serialize the object. Here is an example:
public class Category implements Parcelable {
private String category;
private ArrayList<Item> items;

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public ArrayList<Item> getItems() {
return items;
}

public void setItems(ArrayList<Item> items) {
this.items = items;
}

public Category(){

}

public Category(String _category){
category = _category;
}

@Override
public int describeContents() {
return 0;
}

@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(category);
Bundle b = new Bundle();
b.putParcelableArrayList("items", items);
dest.writeBundle(b);

}

public static final Parcelable.Creator<Category> CREATOR = 
new Parcelable.Creator<Category>() { 
public Category createFromParcel(Parcel in) { 
Category category = new Category();
category.category = in.readString();
Bundle b = in.readBundle(Item.class.getClassLoader());        
category.items = b.getParcelableArrayList("items");

return category;
}

@Override
public Category[] newArray(int size) {
return new Category[size];
}
};
}


To pass an arraylist  of Category to another activity,
intent i = new Intent(_av.getContext(), ItemList.class);
Bundle b = new Bundle();
b.putParcelableArrayList("categories", categories);
b.putInt("index", _index);
i.putExtras(b);
startActivityForResult(i, ITEM_LIST);

To retrieve the data,
Bundle b = this.getIntent().getExtras();

ArrayList<Category> cats = b.getParcelableArrayList("categories");
int index = b.getInt("index");

How to add MessageBox in Android?

Simple message box with no buttons
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setTitle("Packing List");
alertDialog.setMessage("Could not find the file.");
alertDialog.show();

Message box with buttons
AlertDialog deleteAlert = new AlertDialog.Builder(this).create();
deleteAlert.setTitle("Delete List");
deleteAlert.setMessage("Are you sure you want to delete this list?");
deleteAlert.setButton("Yes", new OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {               
//...
}
});
deleteAlert.setButton2("No", new OnClickListener(){

@Override
public void onClick(DialogInterface dialog, int which) {
//...
}
});
deleteAlert.show();