Skip to content Skip to sidebar Skip to footer

Detection Of Swiping On A Normal Android Activity

Is it possible, to detect a swiping to the left/right of the user in a normal Android activity? Or do I have to use a SwipeView?

Solution 1:

If you are looking for a Xamarin/C# example

Implement GestureDetector.IOnGestureListener on your Activity (or view) and calculate the direction of the touch in the OnFling method:

[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")]
publicclassMainActivity : Activity, GestureDetector.IOnGestureListener
{
    GestureDetector gestureDetector;
    constint SWIPE_DISTANCE_THRESHOLD = 100;
    constint SWIPE_VELOCITY_THRESHOLD = 100;

    publicboolOnDown(MotionEvent e)
    {
        returntrue;
    }

    publicboolOnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    {
        float distanceX = e2.GetX() - e1.GetX();
        float distanceY = e2.GetY() - e1.GetY();
        if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
        {
            if (distanceX > 0)
                OnSwipeRight();
            else
                OnSwipeLeft();
            returntrue;
        }
        returnfalse;
    }

    voidOnSwipeLeft()
    {
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Text = "Swiped Left";
    }

    voidOnSwipeRight()
    {
        Button button = FindViewById<Button>(Resource.Id.myButton);
        button.Text = "Swiped Right";
    }

    publicvoidOnLongPress(MotionEvent e)
    {
    }

    publicboolOnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    {
        returnfalse;
    }

    publicvoidOnShowPress(MotionEvent e)
    {
    }

    publicboolOnSingleTapUp(MotionEvent e)
    {
        returnfalse;
    }

    publicboolOnTouch(View v, MotionEvent e)
    {
        return gestureDetector.OnTouchEvent(e);
    }

    publicoverrideboolOnTouchEvent(MotionEvent e)
    {
        gestureDetector.OnTouchEvent(e);
        returnfalse;
    }
    protectedoverridevoidOnCreate(Bundle savedInstanceState)
    {
        base.OnCreate(savedInstanceState);
        SetContentView(Resource.Layout.Main);

        gestureDetector = new GestureDetector(this);
    }
}

Solution 2:

According with https://developer.android.com/training/gestures/detector.html

You can implement GestureDetector.OnGestureListener in your activity class and override onFling or onScroll

@OverridepublicbooleanonScroll(MotionEvent e1, MotionEvent e2, float distanceX,
        float distanceY) {
    Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
    returntrue;
}


@OverridepublicbooleanonFling(MotionEvent event1, MotionEvent event2,
        float velocityX, float velocityY) {
    Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
    returntrue;
}

Post a Comment for "Detection Of Swiping On A Normal Android Activity"