Change Checkbox value without triggering onCheckChanged

AndroidCheckbox

Android Problem Overview


I have setOnCheckedChangeListener implemented for my checkbox

Is there a way I can call

checkbox.setChecked(false);

without triggering the onCheckedChanged

Android Solutions


Solution 1 - Android

No, you can't do it. The onCheckedChanged method is called directly from setChecked. What you can do is the following:

mCheck.setOnCheckedChangeListener (null);
mCheck.setChecked (false);
mCheck.setOnCheckedChangeListener (mListener);

See the source of CheckBox, and the implementation of setChecked:

public void  setChecked(boolean checked) {
	if (mChecked != checked) {
	    mChecked = checked;
	    refreshDrawableState();

	    // Avoid infinite recursions if setChecked() is called from a listener
	    if (mBroadcasting) {
	    	return;
	    }

	    mBroadcasting = true;
	    if (mOnCheckedChangeListener != null) {
	    	mOnCheckedChangeListener.onCheckedChanged(this, mChecked);
	    }

	    if (mOnCheckedChangeWidgetListener != null) {
	    	mOnCheckedChangeWidgetListener.onCheckedChanged(this, mChecked);
	    }

		mBroadcasting = false;            
	}
}

Solution 2 - Android

Add this code inside OnCheckedChangeListener:

if(!compoundButton.isPressed()) {
            return;
}

This will help us to figure out weather checkBox state was changed programmatically or by user action.

Solution 3 - Android

Another possible way to achieve this is by using a custom CheckBox , which will let you choose if you want the listener to be called or not :

public class CheckBox extends AppCompatCheckBox {
    private OnCheckedChangeListener mListener;

    public CheckBox(final Context context) {
        super(context);
    }

    public CheckBox(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public CheckBox(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setOnCheckedChangeListener(final OnCheckedChangeListener listener) {
        mListener = listener;
        super.setOnCheckedChangeListener(listener);
    }

    public void setChecked(final boolean checked, final boolean alsoNotify) {
        if (!alsoNotify) {
            super.setOnCheckedChangeListener(null);
            super.setChecked(checked);
            super.setOnCheckedChangeListener(mListener);
            return;
        }
        super.setChecked(checked);
    }

    public void toggle(boolean alsoNotify) {
        if (!alsoNotify) {
            super.setOnCheckedChangeListener(null);
            super.toggle();
            super.setOnCheckedChangeListener(mListener);
            return;
        }
        super.toggle();
    }
}

Kotlin version, if you prefer:

class CheckBox @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : AppCompatCheckBox(context, attrs, defStyleAttr) {
    private var listener: CompoundButton.OnCheckedChangeListener? = null

    override fun setOnCheckedChangeListener(listener: CompoundButton.OnCheckedChangeListener?) {
        this.listener = listener
        super.setOnCheckedChangeListener(listener)
    }

    fun setChecked(checked: Boolean, alsoNotify: Boolean) {
        if (!alsoNotify) {
            super.setOnCheckedChangeListener(null)
            super.setChecked(checked)
            super.setOnCheckedChangeListener(listener)
            return
        }
        super.setChecked(checked)
    }

    fun toggle(alsoNotify: Boolean) {
        if (!alsoNotify) {
            super.setOnCheckedChangeListener(null)
            super.toggle()
            super.setOnCheckedChangeListener(listener)
            return
        }
        super.toggle()
    }
}

sample usage:

checkBox.setChecked(true,false);

Solution 4 - Android

you use simply setonclickListener , it will works fine and this is very simple method, thanks :)

Solution 5 - Android

For anyone that stumbles across this, one simpler way to do this is to just use a tag on the checkbox and then check that tag on its listener (code is in Kotlin):

checkBox.tag = false
checkBox.setOnCheckedChangeListener{ buttonView, isChecked -> 
    if(checkBox.tag != true) {
        // Do some stuff
    } else {
        checkBox.tag = false
    }

Then when accessing just set the tag to true before you set the isChecked to true when you want to ignore the value change:

checkBox.tag = true
checkBox.isChecked = true

You could also map the tag to a key by using the alternative setTag method that requires a key if you were worried about understandability. But if its all contained to a single class a few comment strings will be more than enough to explain whats happening.

Solution 6 - Android

Using Kotlin's extensions with @Shade answer :

fun CompoundButton.setCustomChecked(value: Boolean,listener: CompoundButton.OnCheckedChangeListener) {
     setOnCheckedChangeListener(null)
     isChecked = value
     setOnCheckedChangeListener(listener)
}

Solution 7 - Android

Is very simple, you just check isPressed inside setOnCheckedChangeListener

Kotlin

switch.setOnCheckedChangeListener { buttonView, isChecked ->
    when {        
        buttonView.isPressed -> {
            foo(isChecked)
        }
    }

Solution 8 - Android

You could use this SafeCheckBox class as your checkbox :

public class SafeCheckBox extends AppCompatCheckBox implements CompoundButton.OnCheckedChangeListener {

    private OnSafeCheckedListener onSafeCheckedListener;

    private int mIgnoreListener = CALL_LISTENER;

    public static final int IGNORE = 0;
    public static final int CALL_LISTENER = 1;

    @Retention(RetentionPolicy.SOURCE)
    @IntDef({IGNORE, CALL_LISTENER})
    public @interface ListenerMode {
    }

    public SafeCheckBox(Context context) {
        super(context);
        init(context);
    }

    public SafeCheckBox(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public SafeCheckBox(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    /**
     * @param checkState     change state of the checkbox to 
     * @param mIgnoreListener true to ignore the listener else listener will be  notified
     */
    public void setSafeCheck(boolean checkState, @ListenerMode int mIgnoreListener) {
        if (isChecked() == checkState) return; //already in the same state no need to fire listener. 

        if (onSafeCheckedListener != null) { // this to avoid a bug if the user listens for the event after using this method and in that case he will miss first check
            this.mIgnoreListener = mIgnoreListener;
        } else {
            this.mIgnoreListener = CALL_LISTENER;
        }
        setChecked(checkState);
    }

    private void init(Context context) {
        setOnCheckedChangeListener(this);
    }


    public OnSafeCheckedListener getOnSafeCheckedListener() {
        return onSafeCheckedListener;
    }

    public void setOnSafeCheckedListener(OnSafeCheckedListener onSafeCheckedListener) {
        this.onSafeCheckedListener = onSafeCheckedListener;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        if (onSafeCheckedListener != null)
            onSafeCheckedListener.onAlwaysCalledListener(buttonView, isChecked);// this has to be called before onCheckedChange
        if (onSafeCheckedListener != null && (mIgnoreListener == CALL_LISTENER)) {
            onSafeCheckedListener.onCheckedChanged(buttonView, isChecked);
        }
        mIgnoreListener = CALL_LISTENER;
    }

    /**
     * Listener that will be called when you want it to be called.
     * On checked change listeners are called even when the setElementChecked is called from code. :(
     */
    public interface OnSafeCheckedListener extends OnCheckedChangeListener {
        void onAlwaysCalledListener(CompoundButton buttonView, boolean isChecked);
    }
}
  • Then you could call :-

setSafeCheck(true,ListenerMode.IGNORE);// OnCheckedChange listener will not be notified

Solution 9 - Android

Set null to changeListener before check radio button. You can set listener again after check radio button.

radioGroup.setOnCheckedChangeListener(null);
radioGroup.check(R.id.radioButton);
radioGroup.setOnCheckedChangeListener(new 

RadioGroup.OnCheckedChangeListener() {
   @Override
   public void onCheckedChanged(RadioGroup radioGroup, @IdRes int i) {
                        
   }
});

Solution 10 - Android

My interpretation which i think is the easiest
May be helpful)

public class ProgrammableSwitchCompat extends SwitchCompat {

    public boolean isCheckedProgrammatically = false;

    public ProgrammableSwitchCompat(final Context context) {
        super(context);
    }

    public ProgrammableSwitchCompat(final Context context, final AttributeSet attrs) {
        super(context, attrs);
    }

    public ProgrammableSwitchCompat(final Context context, final AttributeSet attrs, final int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void setChecked(boolean checked) {
        isCheckedProgrammatically = false;
        super.setChecked(checked);
    }

    public void setCheckedProgrammatically(boolean checked) {
        isCheckedProgrammatically = true;
        super.setChecked(checked);
    }
}

use it

@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean on) {
    if (((ProgrammableSwitchCompat) compoundButton).isCheckedProgrammatically) {
        return;
    }
    //...
    ((ProgrammableSwitchCompat) compoundButton).setCheckedProgrammatically(true);
    //...
    ((ProgrammableSwitchCompat) compoundButton).setCheckedProgrammatically(false);
    //...
}

use will trigger setChecked(boolean) function
that is all

KOTLIN

class MyCheckBox @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = R.attr.switchStyle
) : AppCompatCheckBox(context, attrs, defStyleAttr) {

    var programmatically = false

    override fun setChecked(checked: Boolean) {
        programmatically = false
        super.setChecked(checked)
    }

    fun setCheckedProgrammatically(checked: Boolean) {
        programmatically = true
        super.setChecked(checked)
    }
}

Solution 11 - Android

This is a simple solution I used:
Define a custom listener:

class CompoundButtonListener implements CompoundButton.OnCheckedChangeListener {

    boolean enabled = false;

    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {

    }

    void enable() {
        enabled = true;
    }

    void disable() {
        enabled = false;
    }

    boolean isEnabled() {
        return enabled;
    }
}

Initialization:

CompoundButtonListener checkBoxListener = new CompoundButtonListener() {
    @Override
    public void onCheckedChanged(CompoundButton compoundButton, boolean checked) {
        if (isEnabled()) {
            // Your code goes here
        }
    }
};
myCheckBox.setOnCheckedChangeListener(checkBoxListener);

Usage:

checkBoxListener.disable();

// Some logic based on which you will modify CheckBox state
// Example: myCheckBox.setChecked(true)

checkBoxListener.enable();

Solution 12 - Android

How about this. Try to use Tag in View

mCheck.setTag("ignore");
mCheck.setChecked(true);
mCheck.setTag(null);

and

switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton compoundButton, boolean selected) {

            //If switch has a tag, ignore below
            if(compoundButton.getTag() != null)
                return; 

            if (selected) {
                // do something
            } else {
                // do something else
            }

        }
    });

Solution 13 - Android

Try this one should work for you! You can use this with firebase also!

For get firebase data! Use this!

databaseReference.child(user.getPhoneNumber()).child("Reqs").addValueEventListener(new ValueEventListener() {

        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            SharedPreferences prefs = mContext.getSharedPreferences("uinfo", MODE_PRIVATE);
            String pno = prefs.getString("username", "No name defined");

            if(dataSnapshot.child(pno).getValue(String.class).equals("acc")){
                holder.acc.setChecked(true);
            }else{
                holder.acc.setChecked(false);
            }
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            // Getting Post failed, log a message
            Log.w("dfs", "loadPost:onCancelled", databaseError.toException());
            // ...
        }
    });

After that when user do something!

holder.acc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(isChecked) {
                    if(buttonView.isPressed()) {
                        //your code
                    }
                }
                else {
                    if(buttonView.isPressed()) {
                       //your code
                    }
                }
            }
        });

Solution 14 - Android

> I found all the above answers way too complicated. Why not just create your own flag with a simple boolean?

Just use a simple flag system with a boolean. Create boolean noListener. Whenever you want to turn your switch on/off without running any code (in this example, represented as runListenerCode(), simply set noListener=true before calling switch.setChecked(false/true)

switch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton compoundButton, boolean selected) {
               if (!noListener) { //If we want to run our code like usual
                   runListenerCode();
               } else { //If we simply want the switch to turn off
                   noListener = false;
               }
           });

Very simple solution using simple flags. At the end, we set noListener=false once again so that our code continues to work. Hope this helps!

Solution 15 - Android

I used a ReentrantLock, and lock it whenever I'm setting isChecked:

Kotlin:

// lock when isChecked is being set programmatically
val isBeingProgrammaticallySet = ReentrantLock()

// set isChecked programmatically
isBeingProgrammaticallySet.withLock()
{
    checkbox.isChecked = true
}

// do something only when preference is modified by user
checkbox.setOnCheckedChangeListener()
{
    _,isChecked ->
    if (isBeingProgrammaticallySet.isHeldByCurrentThread.not())
    {
        // do it
    }
}

Solution 16 - Android

isPressed() checked once in onCreate(). add checkbox.isPressed() after your onCheckChangeListener to check every time your check box change.

Solution 17 - Android

I guess using reflection is the only way. Something like this:

CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
try {
    Field field = CompoundButton.class.getDeclaredField("mChecked");
    field.setAccessible(true);
    field.set(cb, cb.isChecked());
    cb.refreshDrawableState();
    cb.invalidate();
} catch (Exception e) {
    e.printStackTrace();
}

Solution 18 - Android

My solution written in java based on @Chris answer:

chkParent.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.getTag() != null){
                    buttonView.setTag(null);
                    return;
                }
                if(isChecked){
                    chkChild.setTag(true);
                    chkChild.setChecked(false);
                }
                else{
                    chkParent.setChecked(true);
                }
            }
});

chkChild.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if(buttonView.getTag() != null){
                    buttonView.setTag(null);
                    return;
                }
                if(isChecked){
                    chkParent.setTag(true);
                    chkParent.setChecked(false);
                }
                else{
                    chkChild.setChecked(true);
                }
            }
});

2 checkboxes and always one will be checked (one be must checked initially though). Setting tag to true blocks onCheckedChanged listener.

Solution 19 - Android

I didn't really want to be having to pass the listener in each time we set checked changed, nor using enabled as a way of determining whether we should set the value (what happens in the case we have the switch disabled already when setting the value?)

Instead I'm making use of tags with an id and a couple of extension methods you can call:

fun CompoundButton.setOnCheckedWithoutCallingChangeListener(
    listener: (view: CompoundButton, checked: Boolean) -> Unit
) {
    setOnCheckedChangeListener { view, checked ->
        if (view.getTag(R.id.compound_button_checked_changed_listener_disabled) != true) {
            listener(view, checked)
        }
    }
    this.setTag(R.id.compound_button_enabled_checked_change_supported, true)
}

fun CompoundButton.setCheckedWithoutCallingListener(checked: Boolean) {
    check(this.getTag(R.id.compound_button_enabled_checked_change_supported) == true) {
        "Must set listener using `setOnCheckedWithoutCallingChangeListener` to call this method" 
    }

    setTag(R.id.compound_button_checked_changed_listener_disabled, true)
    isChecked = checked
    setTag(R.id.compound_button_checked_changed_listener_disabled, false)
}

Now you can call setCheckedWithoutCallingListener(bool) and it will enforce the correct listener usage.

You can also still call setChecked(bool) to fire the listener if you still need it

Solution 20 - Android

Here's a version of the tag technique that is very easy to use.

Usage:

// Pass true to enable bypassing of the listener
button.setOnCheckedChangedListener(true) { _, isChecked ->
    // your usual code
}

// Use extension function to set the value and bypass the listener
button.setCheckedSilently(true)

It's done with a couple of utility extension functions:

inline fun CompoundButton.setOnCheckedChangeListener(canBypass: Boolean, crossinline listener: (CompoundButton, Boolean) -> Unit) {
    if (canBypass) {
        setOnCheckedChangeListener { view, isChecked ->
            if (view.tag != ListenerBypass) {
                listener(view, isChecked)
            }
        }
    } else {
        setOnCheckedChangeListener { view, isChecked -> listener(view, isChecked) }
    }
}

fun CompoundButton.setCheckedSilently(isChecked: Boolean) {
    val previousTag = tag
    tag = ListenerBypass
    this.isChecked = isChecked
    tag = previousTag
}

object ListenerBypass

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionArchie.bpgcView Question on Stackoverflow
Solution 1 - AndroidShadeView Answer on Stackoverflow
Solution 2 - AndroidkrisDrOidView Answer on Stackoverflow
Solution 3 - Androidandroid developerView Answer on Stackoverflow
Solution 4 - AndroidRohitView Answer on Stackoverflow
Solution 5 - AndroidChrisView Answer on Stackoverflow
Solution 6 - AndroidMickey MouseView Answer on Stackoverflow
Solution 7 - AndroidJorisView Answer on Stackoverflow
Solution 8 - AndroidkrishanView Answer on Stackoverflow
Solution 9 - AndroidgencaysahinnView Answer on Stackoverflow
Solution 10 - AndroidVladView Answer on Stackoverflow
Solution 11 - AndroidvovahostView Answer on Stackoverflow
Solution 12 - AndroidareemaView Answer on Stackoverflow
Solution 13 - AndroidLakpriya SenevirathnaView Answer on Stackoverflow
Solution 14 - AndroidRuchir BaroniaView Answer on Stackoverflow
Solution 15 - AndroidEricView Answer on Stackoverflow
Solution 16 - AndroidamiraliView Answer on Stackoverflow
Solution 17 - AndroidZielonyView Answer on Stackoverflow
Solution 18 - AndroidMakaleleView Answer on Stackoverflow
Solution 19 - AndroiddaentechView Answer on Stackoverflow
Solution 20 - AndroidTenfour04View Answer on Stackoverflow