Toggle checkbox programmatically
Solution 1:
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" />
Solution 2:
First of all go through my this answer: Android listview with check boxes?
Nice as you want to implement checked/unchecked check boxes in ListView, you just need to implement below lines in getView() method:
// also check this lines in the above example
ViewHolder holder = (ViewHolder) view.getTag();
holder.checkbox.setChecked(list.get(position).isSelected());
Also check the getView() method for the implementation of event on CheckBox residing inside the ListView:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
if (convertView == null) {
LayoutInflater inflator = context.getLayoutInflater();
view = inflator.inflate(R.layout.rowbuttonlayout, null);
final ViewHolder viewHolder = new ViewHolder();
viewHolder.text = (TextView) view.findViewById(R.id.label);
viewHolder.checkbox = (CheckBox) view.findViewById(R.id.check);
viewHolder.checkbox
.setOnCheckedChangeListener(
new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
Model element = (Model) viewHolder.checkbox
.getTag();
element.setSelected(buttonView.isChecked());
}
});
view.setTag(viewHolder);
viewHolder.checkbox.setTag(list.get(position));
} else {
view = convertView;
((ViewHolder) view.getTag()).checkbox.setTag(list.get(position));
}
ViewHolder holder = (ViewHolder) view.getTag();
holder.text.setText(list.get(position).getName());
holder.checkbox.setChecked(list.get(position).isSelected());
// ......
}
Solution 3:
Have you tried the following?
@Override
public void onListItemClick(ListView listView, View view, int position, long id) {
//Invert the checkbox-status
((CheckedTextView) view.findViewById(R.id.text1)).setChecked(!isChecked());
return;
}