'How to prevent a CheckBox from being checked?

I would like to be able to prevent a CheckBox from being selected (or to set it back to unselected), when the CheckBox is clicked

How can I achieve this?

I do not want to simply disable the checkbox. I want the user to think it is checkable, but when the user tries to check it... then I will (possibly) prevent the checkbox from being checked and display a message.



Solution 1:[1]

you can do something like this:

cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){

    @Override
    public void onCheckedChanged(CompoundButton buttonView,
    boolean isChecked) {
    if(isChecked){
        cb.setChecked(false);
        // Code to display your message.
        }
    }
});

Solution 2:[2]

Just add the android:clickable="false" attribute in the layout xml.

So for me I have:

    <CheckBox
        android:id="@+id/server_is_online"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:checked="true"
        android:clickable="false"
        android:text="@string/server_is_online"
        android:textSize="23sp" />

and it works fine.

No that's probably not how you're supposed to use a checkbox, but I am using this as a dirty hack in the prototyping stage in lieu of having a nice icon with a green tick for all good, and an evil red cross for end of the world :)

Solution 3:[3]

Just set it to never being clicked

cb.setClickable(false);

Solution 4:[4]

Try the following

    CheckBox repeatChkBx =
    ( CheckBox ) findViewById( R.id.repeat_checkbox );
repeatChkBx.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
    {
        if ( isChecked )
        {
           repeatChkBx.setChecked(false); // perform logic of opening message
        }

    }
});

Solution 5:[5]

this code perfect work for me

mCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {

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

                if ( isChecked==true )
                {
                    buttonView.setChecked(false);
                }
                else
                {
                    buttonView.setChecked(true);
                }

        }

    }); //this code through user cant check box check/uncheck 

Solution 6:[6]

Try this

<CheckBox?    
    **android:background="@android:color/transparent"?
    **android:clickable="false"?
    android:id="@+id/login_access_tick"?    
    android:layout_width="wrap_content"?    
    android:layout_height="wrap_content"/>

android:background for removing on click ripple effect

android:clickable="false"? for making it not clickable

Solution 7:[7]

In Android CompoundButton class perfomClick() toggles checked state of button.

@Override
    public boolean performClick() {
        toggle();

        final boolean handled = super.performClick();
        if (!handled) {
            // View only makes a sound effect if the onClickListener was
            // called, so we'll need to make one here instead.
            playSoundEffect(SoundEffectConstants.CLICK);
        }

        return handled;
    }

You can create a class which extends CheckBox and override performClick() method if you want to manually control behaviour. Because clickable=false did not work for me.

Solution 8:[8]

You can try View.TouchListener as a listener to the CheckBox view like so:

 inner class TouchListener : View.OnTouchListener {
        override fun onTouch(view: View, event: MotionEvent): Boolean {
            when (view.id) {
                <Your View id> -> {
                    if (event.action == ACTION_DOWN) {
                        //do your stuff here
                    }
                    return true /*To consume click event so the checkbox doesn't get checked, you can set it checked later once you're done using setChecked(true)*/
                }
            }
            return false
        }
    }

*This snippet is in Kotlin

Solution 9:[9]

Just include android:clickable="false" only, it will work fine.

*Note - Do not include android:focusable attribute

Check out my below working example -

<com.google.android.material.checkbox.MaterialCheckBox
    android:id="@+id/chkSelected"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="8dp"
    android:checked="false"
    android:scaleX="1.1"
    android:scaleY="1.1"
    android:clickable="false"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintBottom_toBottomOf="parent"/>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1
Solution 2 Benoit Duffez
Solution 3 Mário Augusto
Solution 4 codejammer
Solution 5
Solution 6 touhid udoy
Solution 7 toffor
Solution 8 Yasser Khayyal
Solution 9 Dharamveer Mithilesh Gupta