How to prevent a CheckBox from being checked?

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

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

Tags:

Android