Monthly Archives: April 2012

Using Dialog in Android.

Android Dialog, wish it was simple as MessageBox in C#. LOL !

In fact, the way to use Dialog in Android is quite simple.

3 essential steps:

Step 1: Declare a Dialog Builder
In my example activity, onStart():
AlertDialog.Builder builder = new AlertDialog.Builder(this);

Step 2: Logic to handle event raised from Dialog.

DialogInterface.OnClickListener dialogClickListener = new DialogInterface.OnClickListener() {

public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
case DialogInterface.BUTTON_NEGATIVE:


private void enableLocationSettings() {
Intent locationSettingsIntent = new Intent(

Step 3: Show our dialog with our settings.

builder.setMessage("Do you want to turn on GPS ?")
.setPositiveButton("YES", dialogClickListener)
.setNegativeButton("NO", dialogClickListener).setCancelable(false).show();

You could be familiar with Dialog, usually , there are buttons Yes/Accept/OK… and No/Deny/Cancel ….
These 2 groups of buttons called Positive Button and Negative Button group.
What you have to do is handle event corresponding to these buttons.
You may also see the my setting .setCancelable(false)
This is the answer for the question:

How can I prevent users from pressing back button to cancel dialog ?

Here is example project if you want to investigate more details


[ANDROID] How can we enable clicking on a custom ListView in android ?

Working around with ListView, the beast in Android widget, I found something interesting that I MUST write out 🙂
Ok, here is the description of the app’s context:
I need an app that use a custom Listview ( which means a custom List Adapter , and your own list-item layout). In each list item, there are a TextView as a name label, and a CheckBox.
When I click on the row, there will be a toast appear show the name on the label.

Let’s get our hand wet with code:
For the layout, we have list_item.xml

For the main.xml layout:

For the

package example.listview.handlingevent;

import java.util.ArrayList;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.ImageView;
import android.widget.TextView;

public class CustomListViewAdapter extends BaseAdapter {
private LayoutInflater inflater;
private ArrayList data;
private int resource; // list_item view id resource

* @param context Context of activity calling this method
* @param resource layout for list item
* @param data Type of ArrayList for storing data
public CustomListViewAdapter(Context context, int resource,
ArrayList data) {
this.inflater = (LayoutInflater)context
this.resource = resource; = data;

public int getCount() {
return data.size();

public Object getItem(int position) {
return data.get(position);

public long getItemId(int posistion) {
return posistion;

public View getView(int position, View convertView, ViewGroup parent) {
View view;
if (convertView == null) { // first time creating item
view = inflater.inflate(resource, parent, false); // no attaching to
// root
} else {
view = convertView;
return bindData(view, position);

private View bindData(View view, int position) {
if (data.get(position) == null) {
return view; // nothing to set

MyItem item = data.get(position);

TextView text = (TextView) view.findViewById(;

CheckBox check = (CheckBox)view.findViewById(;
return view;


For the data item with

package example.listview.handlingevent;

public class MyItem {
public String text;
public boolean isSelected;

public MyItem(){

public MyItem(String text) {
this.text = text;
this.isSelected = false;

public String toString(){
return text;


And, last but not least, the main Activity to mix all this up ,

package example.listview.handlingevent;

import java.util.ArrayList;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class ListViewEventHandlingActivity extends Activity {

public ListView listView;
ArrayList data;
public Button btnMultiSelect;
public Button btnDelete;

OnItemClickListener itemClickListener;

public void onCreate(Bundle savedInstanceState) {

listView = (ListView) findViewById(;
btnMultiSelect = (Button) findViewById(;
btnDelete = (Button) findViewById(;



public void registerComponents() {

data = new ArrayList();
MyItem item1 = new MyItem("AAAAAA");
MyItem item2 = new MyItem("BBBBBB");
MyItem item3 = new MyItem("CCCCCC");
MyItem item4 = new MyItem("DDDDDD");
MyItem item5 = new MyItem("EEEEEE");
MyItem item6 = new MyItem("FFFFFF");

CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, data);

itemClickListener = new OnItemClickListener() {

public void onItemClick(AdapterView parent, View view,
int position, long id) {



You can download my whole project in Mediafire here:

Note: This Android project I created in Android version 2.3.3 .

Weird question mark in Android ?

Tonight , when working on implementing custom list-view in Android, I run into a weird question mark in following code:

It turns out that “?” is to indicate the pre-defined style attribute called “listPreferredItemHeight” located in namespace “android”.

That’s all 🙂

Find the quick answer from stackoverlow here :