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
2. Select target build version for this application
3. Enter the application details
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
7. Download the source code

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

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
Hai…….
Your code really help me….thanks….
Can I use toggle button instead of checkbox in the same code…?
Hi,
We can use toggle button, instead of checkbox in a listview. This is discussed in the article titled Enabling Multi Selection mode in ListView by adding ToggleButton using custom layout in Android
Hai george thanks for your reply…….
Thank you so much!
Really straightforward and clear. And it works!
Great job.