Toggle checkbox programmatically

DashRantic picture DashRantic · Nov 9, 2011 · Viewed 47.9k times · Source

I have a ListView of items that need to be checkable/uncheckable. I have set up an ArrayAdapter that is currently using android.R.layout.simple_list_item_multiple_choice as the row, and everything displays just fine. I am also able to properly get the clicks on this item. However, the Checkbox in the UI does not toggle when the item is selected. I've been trying to figure this out for a while, can anyone point me in the right direction? I just want to know how to force the UI to update to reflect the changed state of the checkbox.

I can provide code if needed, but I'm trying to look for something very specific here, so I figure posting a bunch of my code wouldn't be of much help.

Thanks!

Answer

MaxEcho picture MaxEcho · May 26, 2014

Programmatically in java code

CheckBox mCheckBox = (CheckBox) findViewById(R.id.checkBox);

mCheckBox.setChecked(true); //to check
mCheckBox.setChecked(false); //to uncheck

In android XML

android:checked="true" //to check
android:checked="false" //to uncheck

as

<CheckBox
    android:id="@+id/checkBox"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="true"
    android:text="Checkbox Item" />