Home » Android » android – How to prevent a CheckBox from being checked?

android – How to prevent a CheckBox from being checked?

Posted by: admin April 23, 2020 Leave a comment

Questions:

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.

How to&Answers:

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

Answer:

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 🙂

Answer:

Just set it to never being clicked

cb.setClickable(false);

Answer:

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
        }

    }
});

Answer:

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 

Answer:

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.