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
2. Select build target as Android version 4.0 ( API level 14 )
3. Enter application related 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
7. Download the source code
8. Reference
http://developer.android.com/guide/index.html





how to add conditions on checked item if i want to put the checked items inside sqlite database
Very much Thanks …. neat structured , unambiguous solution…respecting your helping mind.
oh sir thank you thank you soooo much… it really helps a lot….
thank you sir very good code
it’s a very good explain this example
good explanation
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
Hi sir, i want to list contacts with checkbox and after that want to handle selected items .. can you help me thank you..