1

I don't have a slider menu/drawer, But I would like to make use of the slider event, in which normally apps show a drawer from the left side of screen. I need to do some different task when user do this gesture.

Is it possible to listen to this event? if yes How ?

ted
  • 3,911
  • 3
  • 26
  • 49

1 Answers1

4
import android.app.Activity;
import android.util.Log;
import android.view.MotionEvent;

public class SlidableActivity extends Activity {
    protected static final String TAG = "SlidableActivity";
    private static final int ACTION_TYPE_DEFAULT = 0;
    private static final int ACTION_TYPE_UP = 1;
    private static final int ACTION_TYPE_RIGHT = 2;
    private static final int ACTION_TYPE_DOWN = 3;
    private static final int ACTION_TYPE_LEFT = 4;
    private static final int SLIDE_RANGE = 100;
    private float mTouchStartPointX;
    private float mTouchStartPointY;
    private int mActionType = ACTION_TYPE_DEFAULT;

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getRawX();
        int y = (int) event.getRawY();
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mTouchStartPointX = event.getRawX();
            mTouchStartPointY = event.getRawY();
            break;
        case MotionEvent.ACTION_MOVE:
            if (mTouchStartPointX - x > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_LEFT;
            } else if (x - mTouchStartPointX > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_RIGHT;
            } else if (mTouchStartPointY - y > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_UP;
            } else if (y - mTouchStartPointY > SLIDE_RANGE) {
                mActionType = ACTION_TYPE_DOWN;
            }
            break;
        case MotionEvent.ACTION_UP:
            if (mActionType == ACTION_TYPE_UP) {
                slideUp();
            } else if (mActionType == ACTION_TYPE_RIGHT) {
                slideToRight();
            } else if (mActionType == ACTION_TYPE_DOWN) {
                slideDown();
            } else if (mActionType == ACTION_TYPE_LEFT) {
                slideToLeft();
            }
            break;
        default:
            break;
        }
        return true;
    }

    protected void slideToLeft() {
        Log.d(TAG, "slideToLeft() was called.");
    }

    protected void slideToRight() {
        Log.d(TAG, "slideToRight() was called.");
    }

    protected void slideUp() {
        Log.d(TAG, "slideUp() was called.");
    }

    protected void slideDown() {
        Log.d(TAG, "slideDown() was called.");
    }
}

Checkout this link too

Community
  • 1
  • 1
Anoop M Maddasseri
  • 10,213
  • 3
  • 52
  • 73
  • So we have to write the logic for the event, There is nothing like - onSwipeMenuOpened or onSwipemenuClosed ? – ted Jul 14 '15 at 07:00
  • your not using drawer right .so,overriding onTouchEvent is good enough for detect swipe events. – Anoop M Maddasseri Jul 14 '15 at 07:03
  • Thanks but Actualyl I am interested only in that particular event. Slide right from the left end. this will handle and do for all the slide right and slide left ? – ted Jul 14 '15 at 07:17
  • Great, it works! But I think you should restore `mActionType = ACTION_TYPE_DEFAULT;` within any method `slideToRight()`, `slideToLeft()`, `slideUp()` and `slideDown()`, otherwise any next (even basic) touch will be considered as a sliding event. – logi-kal Sep 15 '19 at 10:01