I want to be able to swipe anywhere on the screen to call a certain function. But I also have Buttons in Linear Layouts that I want to be able to click on. If I swipe on a Button I want onInterceptTouchEvent to Intercept the call to the Button's onTouchEvent and perform a swipe action. And if I simply click on a Button I do not want the onInterceptTouchEvent to be called. Rather, I want the Button's onTouchEvent to be called and perform a Button click
But I get errors when I try implementing onInterceptTouchEvent.
Here is my code:
public class Game extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.game_activity); //other code.... } } @Override public boolean onInterceptTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: break; case MotionEvent.ACTION_CANCEL: case MotionEvent.ACTION_UP: swipeScreen(); //if action recognized as swipe then swipe break; case MotionEvent.ACTION_MOVE: float x = event.getX(); float y = event.getY(); float xDelta = Math.abs(x - mLastX); float yDelta = Math.abs(y - mLastY); if (yDelta > xDelta) { return true; } break; } return false; } @Override public boolean onTouchEvent(MotionEvent event) { ButtonOnClick(); //if not a swipe, then button click return true; } First the error says: The method onInterceptTouchEvent(MotionEvent) of type Game must override or implement a supertype method
Then instead of return true I change the code to:return super.onInterceptTouchEvent(event) but then I get an error saying: The method onInterceptTouchEvent(MotionEvent) is undefined for the type Activity
Can someone please help?
onInterceptTouchEvent()method is a member of theViewGroupclass. To implement it, you'll have to subclass aViewGroupto be used in your layout.