[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
this.resource = resource;
this.data = 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(R.id.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(){

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;

public void onCreate(Bundle savedInstanceState) {

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



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: http://www.mediafire.com/?h1acmg44utxtg53

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


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: