Event Listener for Checkboxes in ListView

June 14, 2012
By

In this article, we will see, how to write the code for handling the checkbox state changes in listview.

If you want to use ToggleButton instead of checkboxes, please see the article titled “Enabling Multi Selection mode in ListView by adding ToggleButton using custom layout in Android“.


1. Create a new Android Project namely ListViewEventListener

New Android Project

Figure 1: New Android Project


2. Select target build version for this application

Figure 2: Select Buid Target Android 2.3 ( API Level 10 )


3. Enter the application details

New Android Project

Figure 3 : New Android Project


4. Open and update the file /res/layout/main.xml with the given below code


<?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" >

    <ListView
        android:id="@+id/listview"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:choiceMode="multipleChoice"
    />
</LinearLayout>

5. Open and update the file /src/in/wptrafficanalyzer/listvieweventlistener/MainActivity.java


package in.wptrafficanalyzer.listvieweventlistener;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity extends Activity {
    String[] countries = new String[] {
        "India",
        "Pakistan",
        "Sri Lanka",
        "China",
        "Bangladesh",
        "Nepal",
        "Afghanistan",
        "North Korea",
        "South Korea",
        "Japan"
    };

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        // Get the object reference of listview from the layout
        ListView listView = ( ListView ) findViewById(R.id.listview);

        // Instantiating an array adapter for listview
        ArrayAdapter< String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, countries);
        listView.setAdapter(adapter);

        //Defining an item click listener
        OnItemClickListener itemClickListener = new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int position, long id) {
                // AdapterView is the parent class of ListView
                ListView lv = (ListView) arg0;
                if(lv.isItemChecked(position)){
                    Toast.makeText(getBaseContext(), "You checked " + countries[position], Toast.LENGTH_SHORT).show();
                }else{
                    Toast.makeText(getBaseContext(), "You unchecked " + countries[position], Toast.LENGTH_SHORT).show();
                }
           }
        };

        // Setting the ItemClickEvent listener for the listview
        listView.setOnItemClickListener(itemClickListener);
    }
}


6. Execute the application in Android Virtual Machine

Event Listener for Listview

Figure 4: Event Listener for Listview

 


7. Download the source code


8. Reference

http://developer.android.com/guide/index.html


How to hire me?

I am George Mathew, working as software architect and Android app developer at wptrafficanalyzer.in

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to info@wptrafficanalyzer.in.

My other blogs
store4js.blogspot.com


Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :



Tags:

4 Responses to Event Listener for Checkboxes in ListView

  1. Haris on September 3, 2012 at 4:45 pm

    Hai…….
    Your code really help me….thanks….

    Can I use toggle button instead of checkbox in the same code…?

  2. Felipe on July 12, 2013 at 10:22 am

    Thank you so much!

    Really straightforward and clear. And it works!

    Great job.

Leave a Reply

Your email address will not be published. Required fields are marked *

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner