I use a checkbox in my app as a button turn something on or off. But the action (load a file from the network) is done in an async task so I don't want the check to come on until the async task finishes successfully, like this
protected void onPostExecute(String result) {
if(result==null) {
return;
}
// loaded ok, turn on check mark
MainActivity.mMp3Cb.setChecked(true);
The problem is, setChecked(true)
causes OnCheckedChangeListener
to fire again as if it were user input
Is there a way to avoid this? or at least detect it in onCheckedChanged
?
thanks
So, the method to know if the check box is checked is : (CheckBox) yourCheckBox. isChecked() it returns true if the check box is checked.
You can call isChecked() on a checkbox to get its status.
You can toggle the checkbox by doing this: checkbox. setChecked(! checkbox.
You can use the isPressed()
method of the button view object. Here's an example of a toggle button in Android. buttonView.isPressed()
is only true if the user clicked on the button.
@Override
public synchronized void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (buttonView.isPressed()) {
// human input
} else {
// result of setChecked(boolean)
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With