Create ListView with Multiple CheckBox in Android Example Tutorial

How to get retrieve multiple check boxes values from ListView using setOnItem ClickListener() getChecked ItemPositions().

In this tutorial we are using the simplest method to to create multiple check boxes in android application without use of any CheckBox widget. We are using ListView widget another property called as android:choiceMode=”multipleChoice” with the combination of simple_list_item_multiple_choice. This will automatically convert our normal listview to CheckBox list . With this android developer can get multiple choices from android application user with one single layout. So here is the complete step by step tutorial for Create ListView with Multiple CheckBox in Android Example Tutorial.

android-project-download-code-button

How to Create ListView with Multiple CheckBox in Android Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.listviewwithcheckbox_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.SparseBooleanArray;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    ListView listview ;
    String[] ListViewItems = new String[] {
            "ListView ITEM-1",
            "ListView ITEM-2",
            "ListView ITEM-3",
            "ListView ITEM-4",
            "ListView ITEM-5",
            "ListView ITEM-6",
            "ListView ITEM-7",
            "ListView ITEM-8",
            "ListView ITEM-9",
            "ListView ITEM-10"

    };

    SparseBooleanArray sparseBooleanArray ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        listview = (ListView)findViewById(R.id.listView);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>
                (MainActivity.this,
                        android.R.layout.simple_list_item_multiple_choice,
                        android.R.id.text1, ListViewItems );

        listview.setAdapter(adapter);

        listview.setOnItemClickListener(new OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                // TODO Auto-generated method stub

                sparseBooleanArray = listview.getCheckedItemPositions();

                String ValueHolder = "" ;

                int i = 0 ;

                while (i < sparseBooleanArray.size()) {

                    if (sparseBooleanArray.valueAt(i)) {

                        ValueHolder += ListViewItems [ sparseBooleanArray.keyAt(i) ] + ",";
                    }

                    i++ ;
                }

                ValueHolder = ValueHolder.replaceAll("(,)*$", "");

                Toast.makeText(MainActivity.this, "ListView Selected Values = " + ValueHolder, Toast.LENGTH_LONG).show();

            }
        });

    }
}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.listviewwithcheckbox_android_examplescom.MainActivity">

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/listView"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:choiceMode="multipleChoice" />
</RelativeLayout>

Screenshots:

android-CheckBox-listview-1

Create ListView with Multiple CheckBox in Android Example Tutorial

Click here to download Create ListView with Multiple CheckBox in Android Example Tutorial project with source code.

Leave a Reply

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