3

I have a layout with couple checkboxes. When one checkbox gets checked the others are set to CHECKED=true and ENABLED=false. Now I want the user to be able to tap on any of that disabled checkboxes and if he does that, one is set to enabled and checked and all the other are disabled.

The onTouchListener as well as the onClickListener doesn't seem to be called when the checkbox is set to ENABLED=false. Can anyone help?

Iulia Barbu
  • 1,522
  • 12
  • 25
Filip Majernik
  • 7,700
  • 13
  • 45
  • 53
  • how about the listener ChkBx.setOnCheckedChangeListener.. Does that gets called? – Gridtestmail Jan 04 '13 at 13:45
  • 1
    enabled=false means that the checkbox isn't clickable at all. sounds to me that what you want to do is simply to change checked to false. have you considered using radio buttons instead? might be a better solution in the case that you only want one item at the time to be selected. – TofferJ Jan 04 '13 at 13:47
  • Why should user click on disabled checkbox? I believe the users will not understand this "feature" :) – alex Feb 01 '17 at 11:40
  • @TofferJ i have done same. – Arbaz.in Mar 19 '20 at 11:33
  • @dieter for user we can display toast OR alert dialog. – Arbaz.in Mar 19 '20 at 11:33

2 Answers2

0

Usually the behavior for a situation like that is a question-mark in Android. One thing you may be able to do is to put the CheckBox from something descended from ViewGroup (One of the many layouts like FrameLayout, for example) and use setOnClickListener on it.

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
Joe Plante
  • 6,308
  • 2
  • 30
  • 23
  • 2
    This works pretty well, however, the Click event is than only generated from the layout. That means, if I click on the checkbox it is still not propagated. Is there any setting to make the whole layout clickable? – Filip Majernik Jan 04 '13 at 15:45
0

You can not receive events on a disabled checkbox. If you put the disabled checkbox on a layout like FrameLayout, you can receive the events when you click on the layout but not in the disabled checkbox. The best way if you want to capture events on a disabled checkbox is to simulate a disabled checkbox simply, and capture a long click event to activate again, for example.

What I have done is a checkbox with white text color but beginning with grey text color and unchecked, with a boolean stopper variable which you check before on every onCheckedChanged method. Checkbox is never checked unless you change the boolean stopper variable. You can press on checkbox many times as you want and nothing happens. It only appears to be disabled but when you press a long click you unblock the boolean stopper variable and change the grey text color checkbox to white like a normal checkbox. You can change the stopper variable when you want and "disabling it again"

In color.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
   <color name="white">#FFFFFF</color>
   <color name="grey">#808080</color>
</resources>

In main.xml:

<CheckBox
      android:id="@+id/checkbox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:textColor="@color/white"
      android:text="text" />

In the main.java code, onCreate method:

//define a boolean stopper variable to check on event
boolean chkActivated = false; 

checkbox = (CheckBox) findViewById(R.id.checkbox1);
checkbox.setTextColor(getResources().getColorStateList(R.color.grey));
checkbox.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener() {
     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
         if(chkActivated){
             if (isChecked) {
                 //Do everthing you want when checked
             }else{
                //Do everthing you want when unchecked
             }
         }else{
             checkbox.setChecked(false);
             Toast.makeText(Activity.this, "It is disabled. to activate press long click"), Toast.LENGTH_SHORT).show();
         }
      }
});

checkbox.setOnLongClickListener(new OnLongClickListener() { 
   @Override
   public boolean onLongClick(View v) {
       chkActivated = true;
       checkbox.setTextColor(getResources().getColorStateList(R.color.white));
       checkbox.setChecked(true);
       return true;
   }
});

Hope this helps you

Vasily Kabunov
  • 6,511
  • 13
  • 49
  • 53
JJ_1
  • 35
  • 5