Search code examples
androidlistviewcheckboxandroid-arrayadapter

Android: How to detect when a disabled Checkbox is touched?


I require that sometimes my Checkboxes in a ListView are not tickable. I'm using setEnabled(false).

Is there anyway to fire a toast message when the user still clicks on the disabled Checkbox?

I am also using a custom ArrayAdapter overriding the getView method.


Solution

  • You can :) using a helper class that extends CheckBox and overrides onTouchEvent, kind of like:

    public class DisabledTouchableCheckbox extends CheckBox {
    
        public DisabledTouchableCheckBox(Context context, AttributeSet attrs) {
            super(context, attrs);
        }
    
        @Override
        public boolean onTouchEvent(MotionEvent event) {
            if ((!isEnabled()) && (event.getAction()==MotionEvent.ACTION_DOWN)) 
                showToast();
            return super.onTouchEvent(event);
        }
    
        private void showToast() {
                ... show your toast
        }
    
    }
    

    Then in your XML's you have to change <CheckBox> for <com.YOURPACKAGE.DisabledTouchableCheckBox>