Android: ItemClickListner with CheckBox or RadioButton.

Android: ItemClickListner with CheckBox or RadioButton.

Lets take a scenario to understand the need of a listview which will have checkbox embedded in it.

Consider the flipkart mobile app, in that we want to compare 2 or more products, in order to do that I will go on selecting the items which I want to compare and then click on the compare button.

 

So we have multiple items in that and hence we cant use the android built in types for creating the list view. Hence we need to follow the below mentioned approach.

 

  1. Create an activity, which contains 2 listviews, one listview for displaying all available mobiles and another listview for displaying selected mobiles.
  2. Create an adapter for filling the mobile list for the above mentioned listviews.
  3. The layout which will be used for creating row which will make ItemClickListner work is as follows,

 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:descendantFocusability="blocksDescendants"
    android:focusable="false"
    android:focusableInTouchMode="false"
    android:clickable="false">

    <CheckBox
        android:id="@+id/selectMob"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"
        />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="2"
        android:layout_toRightOf="@id/selectMob"
        android:layout_alignTop="@id/selectMob">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Nokia"
        android:id="@+id/make"
        android:layout_gravity="center_horizontal"
        android:layout_weight="1"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Lumia"
            android:id="@+id/model"
            android:layout_weight="1"/>
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:weightSum="3"
        android:layout_below="@id/selectMob">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Android"
            android:id="@+id/os"
            android:layout_gravity="center_horizontal"
            android:layout_weight="2"/>
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="20000"
            android:id="@+id/price"
            android:layout_weight="1"/>
        </LinearLayout>

</RelativeLayout>

Observe the following attributes for Checkbox,

        android:focusable="false"
        android:focusableInTouchMode="false"
        android:clickable="false"

These make the checkbox non clickable and eventually the event will be transferred to its parent i.e the row and hence the ItemClickListner will be invoked.

You can download the files here