ListView with Checkboxes in Android

June 14, 2012
By

In this article, we are going to discuss, how to create and use checkboxes in ListView widget of Android.


1. Create a new Android project named ListViewCheckbox

Create New Android Project

Figure 1: Create new android project


2. Select build target as Android version 4.0 ( API level 14 )

Select Android Api Level

Figure 2: Select android api level


3. Enter application related details

Application Details

Figure 3: Enter application details


4. Open and update the file /res/layout/main.xml with the given below code. The highlighted code specifies that, this list view is  multi-selectable

<?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/listviewcheckbox/MainActivity.java with the given below code

package in.wptrafficanalyzer.listviewcheckbox;

import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;

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);

        // The checkbox for the each item is specified by the layout android.R.layout.simple_list_item_multiple_choice
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, countries);

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

        // Setting adapter to the listview
        listView.setAdapter(adapter);

    }
}

6. Application in execution

 

ListView with Checkboxes

Figure 4: ListView with Checkboxes


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:

9 Responses to ListView with Checkboxes in Android

  1. abhishek on November 6, 2012 at 1:06 pm

    how to add conditions on checked item if i want to put the checked items inside sqlite database

  2. noufal on November 14, 2012 at 8:09 pm

    Very much Thanks …. neat structured , unambiguous solution…respecting your helping mind.

  3. vicky on December 20, 2012 at 2:44 am

    oh sir thank you thank you soooo much… it really helps a lot….

  4. sreedhu on January 22, 2013 at 12:35 pm

    thank you sir very good code

  5. ashish adesaar on January 24, 2013 at 2:52 pm

    it’s a very good explain this example

  6. ashish adesaar on January 24, 2013 at 3:37 pm

    good explanation

  7. Gino on January 25, 2013 at 9:44 pm

    Hello can you build an application that saves the items and the listview to a file and saves the checkbox statement also.
    because I cannot find an example.

    best regards

  8. Sankaran on May 13, 2013 at 11:12 am

    Hi sir, i want to list contacts with checkbox and after that want to handle selected items .. can you help me thank you..

  9. Aakash on August 5, 2013 at 4:50 pm

    Hi Sir, i want to delete two or more row using checkbox from sqlite database in android ….can you help me ….

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