Warning: Custom Stance Overrides Ontouchevent Simply Non Performclick Together With #Ontouchevent Should Telephone Weep Upward #Performclick When A Click Is Detected

Posted on

If yous re-create the final instance of “Custom View, notice deport upon to toggle color“, yous volition live on warned on onTouchEvent() amongst message of “MyView overrides onTouchEvent only non performClick“!

Then yous override performClick(), yous volition live on warned in 1 trial again amongst “MyView#onTouchEvent should telephone telephone …/MyView#performClick when a click is detected“!!!

Android document convey advert nearly “Handling custom deport upon events“:

Custom thought controls may postulate non-standard deport upon effect behavior. For example, a custom command may role the onTouchEvent(MotionEvent) listener method to notice the ACTION_DOWN too ACTION_UP events too trigger a especial click event. In gild to hold compatibility amongst accessibility services, the code that handles this custom click effect must create the following:

  1. Generate an appropriate AccessibilityEvent for the interpreted click action.
  2. Enable accessibility services to perform the custom click activity for users who are non able to role a deport upon screen.

To grip these requirements inwards an efficient way, your code should override the performClick() method, which must telephone telephone the super implementation of this method too and hence execute whatever actions are required yesteryear the click event. When the custom click activity is detected, that code should too hence telephone telephone your performClick() method. The next code instance demonstrates this pattern.


This video exhibit how to modify MyView.java inwards final postal service to take away the warnings.


After changed, MyView.java become:

package com.example.androidtouchview;  import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View;  world flat MyView extends View {   boolean touchOn;  boolean mDownTouch = false;   world MyView(Context context) {   super(context);   init();  }   world MyView(Context context, AttributeSet attrs) {   super(context, attrs);   init();  }   world MyView(Context context, AttributeSet attrs, int defStyleAttr) {   super(context, attrs, defStyleAttr);   init();  }   soul void init() {   touchOn = false;  }   @Override  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {   setMeasuredDimension(MeasureSpec.getSize(widthMeasureSpec),     MeasureSpec.getSize(heightMeasureSpec));  }   @Override  protected void onDraw(Canvas canvas) {   if (touchOn) {    canvas.drawColor(Color.RED);   } else {    canvas.drawColor(Color.GRAY);   }  }   @Override  world boolean onTouchEvent(MotionEvent event) {   super.onTouchEvent(event);    /*   int activity = event.getAction();    if (action == MotionEvent.ACTION_DOWN) {    touchOn = !touchOn;    invalidate();    furnish true;   }    furnish false;   */   // Listening for the downward too upward deport upon events         switch (event.getAction()) {             instance MotionEvent.ACTION_DOWN:                            touchOn = !touchOn;        invalidate();                               mDownTouch = true;                 furnish true;              instance MotionEvent.ACTION_UP:                 if (mDownTouch) {                     mDownTouch = false;                     performClick(); // Call this method to grip the response, too                                     // thereby enable accessibility services to                                     // perform this activity for a user who cannot                                     // click the touchscreen.                     furnish true;                 }         }         furnish false; // Return fake for other deport upon events  }   @Override  world boolean performClick() {   // Calls the super implementation, which generates an AccessibilityEvent         // too calls the onClick() listener on the view, if whatever         super.performClick();          // Handle the activity for the custom click hither          furnish true;  }  }