Developer
Developer

Reputation: 6350

Uncheck all Check boxes in Android

I have clear button in my layout on click of that i want to uncheck all checkboxes that are inside a linear layout listview .

XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id ="@+id/mainLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/btnClear"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right|center"
                android:gravity="right|center"
                android:text="Remove" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="horizontal">
            <ListView
                android:id="@+id/listview"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="5"
                android:cacheColorHint="@android:color/transparent"
                android:divider="#fff"
                android:dividerHeight="1dp"
                android:fadingEdge="none">
            </ListView>
        </LinearLayout>
    </LinearLayout>
</LinearLayout>

Inside the listview i am inflating a layout that will have checkbox.Now btnClear i want to uncheck all checkboxes .How can we do this please help me in this

Upvotes: 2

Views: 6865

Answers (1)

siriscac
siriscac

Reputation: 1707

Create a for loop going through all the child items of the ListView and check if the View is of type CheckBox and perform setChecked(false) on the same.

for (int i = 0; i < mListView.getCount(); i++) { 
       View mChild = mListView.getChildAt(i);

       //Replace R.id.checkbox with the id of CheckBox in your layout
       CheckBox mCheckBox = (CheckBox) mChild.findViewById(R.id.checkbox);
       mCheckBox.setChecked(false);
}

Upvotes: 5

Related Questions