android CheckBox how to deferentiate between user click and setChecked

android, checkbox

Solution

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)
    }
}

Problem

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

Original source

Related problems