Android Swipe Gesture Detector Code Example Tutorial

How to check detect swipe gesture left to right, top to bottom, bottom to top, right to left in android.

In this tutorial we are going to learn about Swipe Gesture, It is one of base functionality in android mobile phones and also applications because without swipe detection no app or android mobile can work. Android mobile devices is based upon Touch screen and with touch comes movies finger across the screen is everything. Users can move main menus , scroll list views, scroll every thing inside their phones is based on Swipe . So here is the complete step by step tutorial for Android Swipe Gesture Detector Code Example Tutorial.

android-project-download-code-button

Live Demo :

Android Swipe Gesture Detector Code Example Tutorial.

Code for MainActivity.java file.

package com.android_examples.swipedetector_android_examplescom;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.widget.Toast;
import android.view.GestureDetector.OnGestureListener;

public class MainActivity extends AppCompatActivity implements OnGestureListener {

    GestureDetector gestureDetector;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        gestureDetector = new GestureDetector(MainActivity.this, MainActivity.this);

    }

    @Override
    public boolean onFling(MotionEvent motionEvent1, MotionEvent motionEvent2, float X, float Y) {

        if(motionEvent1.getY() - motionEvent2.getY() > 50){

           Toast.makeText(MainActivity.this , " Swipe Up " , Toast.LENGTH_LONG).show();

            return true;
        }

        if(motionEvent2.getY() - motionEvent1.getY() > 50){

            Toast.makeText(MainActivity.this , " Swipe Down " , Toast.LENGTH_LONG).show();

            return true;
        }

        if(motionEvent1.getX() - motionEvent2.getX() > 50){

            Toast.makeText(MainActivity.this , " Swipe Left " , Toast.LENGTH_LONG).show();

            return true;
        }

        if(motionEvent2.getX() - motionEvent1.getX() > 50) {

            Toast.makeText(MainActivity.this, " Swipe Right ", Toast.LENGTH_LONG).show();

            return true;
        }
        else {

            return true ;
        }
    }

    @Override
    public void onLongPress(MotionEvent arg0) {

        // TODO Auto-generated method stub

    }

    @Override
    public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2, float arg3) {

        // TODO Auto-generated method stub

        return false;
    }

    @Override
    public void onShowPress(MotionEvent arg0) {

        // TODO Auto-generated method stub

    }

    @Override
    public boolean onSingleTapUp(MotionEvent arg0) {

        // TODO Auto-generated method stub

        return false;
    }

    @Override
    public boolean onTouchEvent(MotionEvent motionEvent) {

        // TODO Auto-generated method stub

        return gestureDetector.onTouchEvent(motionEvent);
    }

    @Override
    public boolean onDown(MotionEvent arg0) {

        // TODO Auto-generated method stub

        return false;
    }

}

Code for activity_main.xml layout file.

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.android_examples.swipedetector_android_examplescom.MainActivity"
    >


    <TextView
        android:text="Swipe On Screen To Detect Swipe Gesture. "
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:id="@+id/textView"
        android:textSize="25dp"
        android:gravity="center"
        android:textColor="@color/colorPrimaryDark"/>


</RelativeLayout>

Screenshots:

Click here to download Android Swipe Gesture Detector Code Example Tutorial project with source code.

Leave a Reply

Your email address will not be published. Required fields are marked *