Enable and Disble Listview with Some Condition in Android

1.4k Views Asked by At

How to enable all the items in listview when check box is selected and disabled when check box is unchecked.I have used

if(checkbox.isChecked)
{
listview.setEnabled(false)
listview.setClickable(false)
}
else
{
listview.setEnabled(true)
listview.setClickable(true)
}

But it is not working.Any help would be greatly appreciated.

Thanks in Advance:)

2

There are 2 best solutions below

2
On BEST ANSWER
 if(yourcheckbox.isChecked()){
     yourlistview.setClickable(true);} 
 else{
      yourlistview.setClickable(false);} 

You can run this as a background process, in a separate thread so it keeps getting checked, whether the checkbox is checked or not.

1
On

you can use OncheckedChangeListener method of Checkbox class

checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton arg0, boolean isChecked) {
                if(isChecked)
                {
                    listview.setEnabled(false)
                    listview.setClickable(false)
                }
                else
                {
                    listview.setEnabled(true)
                    listview.setClickable(true)
                }

            }
        });