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() {

@Override
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
enableLocationSettings();
break;
case DialogInterface.BUTTON_NEGATIVE:
finish();
break;
default:
finish();
break;
}

}
};

private void enableLocationSettings() {
Intent locationSettingsIntent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(locationSettingsIntent);
}

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

http://www.mediafire.com/?k0zcb6vbadblcrz

Advertisements

[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 CustomListAdapter.java:


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
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.resource = resource;
this.data = data;
}

@Override
public int getCount() {
return data.size();
}

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

@Override
public long getItemId(int posistion) {
return posistion;
}

@Override
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(R.id.text);
text.setText(item.text);

CheckBox check = (CheckBox)view.findViewById(R.id.check);
return view;
}

}

For the data item with MyItem.java:


package example.listview.handlingevent;

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

public MyItem(){
this("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 , ListViewEventHandlingActivity.java


package example.listview.handlingevent;

import java.util.ArrayList;

import android.app.Activity;
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;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

listView = (ListView) findViewById(R.id.listview);
btnMultiSelect = (Button) findViewById(R.id.btnMultiSelect);
btnDelete = (Button) findViewById(R.id.btnDelete);

registerComponents();

}

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");
data.add(item6);
data.add(item5);
data.add(item4);
data.add(item3);
data.add(item2);
data.add(item1);

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

itemClickListener = new OnItemClickListener() {

@Override
public void onItemClick(AdapterView parent, View view,
int position, long id) {
Toast.makeText(getApplicationContext(),
parent.getItemAtPosition(position).toString(),
Toast.LENGTH_SHORT).show();
}
};
listView.setTextFilterEnabled(true);
listView.setOnItemClickListener(itemClickListener);

}

}

You can download my whole project in Mediafire here: http://www.mediafire.com/?h1acmg44utxtg53

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:
android:layout_height="?android:attr/listPreferredItemHeight"
at http://android-developers.blogspot.com/2009/02/android-layout-tricks-1.html

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 : http://stackoverflow.com/questions/7923221/what-androidattr-listpreferreditemheight-is-doing-and-how