Android - Get Selected Items from ListView with CheckBox


In the last tutorial we added checkbox to our list. In this tutorial we will be getting values from the selected chekbox. We also need to add one button. This is second part of checkbox in listview tutorial so if you haven't done that please go back to learn more about creating listviews and layouts for listview.

Screenshot of the layout

How can you achieve this kind of layout ?

By using android:weight with linear layouts we can assign weights to different views i.e we will be telling that how much space should be given to a particular view-item.

For example if there are four TextView. Three of them demands android:weight="1"  and fourth is given no weight. Then the weight of three items will grow as screen grows but fourth one will get only enough space to get occupy.

Below xml will help you to create a layout with weight attribute.

<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/listView1"
        android:layout_width="match_parent"
        android:layout_height="0px"
        android:layout_weight="1" >
    </ListView>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Submit" />

</LinearLayout>

Now for listening to the events you need to add OnItemClickListener. Steps involved are :

  • Create a class and make it implement interface OnItemClickListener
    public class CheckBoxClick implements OnItemClickListener{
    
    	@Override
    	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    		// TODO Auto-generated method stub
    		
    	}	
    }
  • Implement onItemClick() which is an unimplemented method for OnItemClickListener interface.
  • In your onCreate() method of activity add following statement to set event listener for checkbox items.
    listview.setOnItemClickListener(new CheckBoxClick());
  • CheckBoxClick class :
    public class CheckBoxClick implements OnItemClickListener{
    
    	@Override
    	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
    		// TODO Auto-generated method stub
    		CheckedTextView ctv = (CheckedTextView)arg1;
    		if(ctv.isChecked()){
    			Toast.makeText(MainActivity.this, "now it is unchecked", Toast.LENGTH_SHORT).show();
    		}else{
    			Toast.makeText(MainActivity.this, "now it is checked", Toast.LENGTH_SHORT).show();
    		}	
    	}	
    }
  • In the above code we have fetched the view which was clicked. We will check the previous state of this view and depending upon previous state we will do desired action

So it was about adding event to checbox. In the next article we will click the button to get all the checked items from listview and we will pass it to the next activity.