disable child view touches when parent disabled

43 Views Asked by At

I am creating a clickable custom view to replace android.widget.Switch(Switch), using onDraw instead of additional Drawable. When I use Switch previously, if parent is disabled, the Switch is also not clickable, but when I migrated to my custom view, it's still clickable when parent disabled. How can I know if I should disable/ignore clicks?


custom view parent enabled custom view parent enabled

custom view parent disabled (still clickable!) custom view parent disabled

Switch parent disabled (not clickable) android.widget.Switch parent disabled


my code like...

public class MySwitch extends View {

  public boolean onTouchEvent(MotionEvent motionEvent) {
    if (isClickable() && isEnabled()) {
      //do something
    }
  }

  protected void onDraw(Canvas canvas) {
    //draw track and thumb
  }

}
1

There are 1 best solutions below

0
Alexis Tamariz On

You could try validating parent as a ViewGroup as following.

public boolean onTouchEvent(MotionEvent motionEvent) {
    ViewGroup parent = (ViewGroup) getParent();
    Log.d(TAG, "onTouchEvent: parent enabled: " + parent.isEnabled());
}