4

I have a checkbox that can be dynamically disabled and tied to a click listener:

CheckBox chkbox = (CheckBox)findViewById(R.id.chkboxid);

if(condition) {
     chkbox.setEnabled(false);
     chkbox.setOnClickListener(displayPopup);
} else {
     chkbox.setOnClickListener(handleToggle);
}

The purpose of this is if the checkbox is disabled, I want to give users that click on the checkbox more info about why the option is disabled for them.

I have since realized that disabled widgets do not send click events to click listeners. I have since tried setting the LinearLayout it exists in as clickable by doing the following:

CheckBox chkbox = (CheckBox)findViewById(R.id.checkboxName);
LinearLayout layout = (LinearLayout)findViewById(R.id.layoutName);
if(condition) {
     chkbox.setEnabled(false);
     layout.setClickable(true);
     layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
     layout.setOnClickListener(displayPopup);
} else {
     chkbox.setOnClickListener(handleToggle);
}

This works for clicking anywhere inside the LinearLayout except on the disabled checkbox. It is not following the FOCUS_BLOCK_DESCENDANTS setting. I have also tried placing an invisible clickable object over the checkbox but was not successful there either. Any ideas?

EDIT: We're stuck in API lvl 8 right now, or otherwise I'd try lowering the alpha of the checkbox instead of disabling it to at least simulate the appearance of being disabled.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
nagem
  • 158
  • 2
  • 8
  • I just did a similar thing simply using dark gray button tint and text color for the checkbox and made the onClickListener always do ´setChecked(false);´ It looks like disabled although for a milisecond it switches but then is off again and you can do your handling. For my purpose it's good enough. Regards, Ben – Ben Aug 26 '18 at 13:02

2 Answers2

4

Android: Disabled checkbox not responding to click events

I think if CheckBox or other type widget is disabled, no one event can be handled.

But what about to make simple trick. Let CheckBox enabled and when something required is not filled up, always set checked state to false. It will look as "I can't tick a CheckBox, something is bad" and show some warning message via Toast.

checkBox.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        if (somethingIsNotFilledUp) {
            checkBox.setChecked(false);
            Toast.makeText(Main.this, "You didn't fill required fields", Toast.LENGTH_SHORT).show();
        }
        else {
            // do some stuff
        }
    }
});

I would like them to see the difference

You can achieve it by using custom defined checkbox's appearance.

Simon Dorociak
  • 33,374
  • 10
  • 68
  • 106
  • I considered this. The problem is I'd like the user to be able to visually see that it is enabled or disabled since the conditions could change without the user actively doing anything. This might sound confusing, but the condition is true or false based on settings from another user (like an admin), and if the admin user decides to make it available, I would like them to see the difference. – nagem Mar 20 '13 at 22:18
  • 2
    @Megan you can achieve it with custom checkbox background for example :) – Simon Dorociak Mar 20 '13 at 22:28
  • 1
    that's what my team finally decided on. Going to use some info [here](http://stackoverflow.com/questions/3965484/custom-checkbox-image-android) as reference. – nagem Mar 21 '13 at 18:10
2

You can not receive events on a disabled checkbox but I made other trick simulating a disabled checkbox where you can receive checkbox events and enabling it pressing long click on it for example

Simulating disabled checkbox receiving events

Hope this helps you

Community
  • 1
  • 1
JJ_1
  • 35
  • 5