Android Gesture Tutorial – Touch, Scroll and Press on Android Device Screen




Introduction: Android Gesture Tutorial – Touch, Scroll and Press on Android Device Screen

About: My name is Navneet Goel passed out of NSIT,Delhi. I am blogger at and love to do programming in my free time. Visit my site for learning Android st…

Hi Guys. In this post we will learn different type of Gesture Control in Android. For example: You can swipe, Double touch and fling etc. on your smartphone screen and give different functionality when each of above happens.

Note: I suggest you to first run a simple Hello World program before following below steps. You can get that tutorial here:

or at my instructables tutorial here:

Let’s learn different type of Gesture Control in Android. It allows you to make a decent App with fine User Interface. This is the basic building block in most of the Apps. A gesture could be of following types:

1) Touch or Double Touch

2) Double Tap

3) Drag, Swipe or Fling.

4) Long press

In this post we will make a simple App that will display if you are dragging, tapping or scrolling on screen. Let’s start with creating a new project:

Step 1: ​Creating New Project

Please follow following steps:

1) Open Android Studio and make a new project with name “My Application” and company domain (I used my company domain i.e Similarly you can yours).

2) Click Next and choose android version Lollipop. Again Click Next and Choose Blank Activity.

3) Leave all things remaining same and Click Finish.

Now you can see, content_main.xml and strings.xml. If you are not able to see them then click on the left window where these files can easily be located as shown in figure above.

Step 2: Code in

Please type following code in created in step-1:

package com.androidtutorialpoint.myapplication; 
import android.os.Bundle; 
import android.view.GestureDetector; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.widget.TextView; 
import android.widget.Toast; 
public class MainActivity extends AppCompatActivity implements
                                  GestureDetector.OnDoubleTapListener { 
    private TextView output_text;                 // This is added for Text output 
    private GestureDetectorCompat DetectMe;       
    protected void onCreate(Bundle savedInstanceState) { 
        output_text = (TextView) findViewById(;  // Taking reference of text to be displayed on screen
        DetectMe = new GestureDetectorCompat(this,this); 
        Toolbar toolbar = (Toolbar) findViewById(; 
        FloatingActionButton fab = (FloatingActionButton) findViewById(; 
        fab.setOnClickListener(new View.OnClickListener() { 
            public void onClick(View view) { 
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) 
                        .setAction("Action", null).show(); 
// Following functions are overrided to show text when a particular method called.
    public boolean onSingleTapConfirmed(MotionEvent e) { 
        return true; 
    public boolean onDoubleTap(MotionEvent e) { 
        return true; 
    public boolean onDoubleTapEvent(MotionEvent e) { 
        return true; 
    public boolean onDown(MotionEvent e) { 
        return true; 
    public void onShowPress(MotionEvent e) { 
    public boolean onSingleTapUp(MotionEvent e) { 
        return true; 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
        return true; 
    public void onLongPress(MotionEvent e) { 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        return true; 
    public boolean onTouchEvent(MotionEvent event){ 
        return super.onTouchEvent(event); 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(, menu); 
        return true; 
    public boolean onOptionsItemSelected(MenuItem item) { 
        // Handle action bar item clicks here. The action bar will 
        // automatically handle clicks on the Home/Up button, so long 
        // as you specify a parent activity in AndroidManifest.xml. 
        int id = item.getItemId(); 
        //noinspection SimplifiableIfStatement 
        if (id == { 
            return true; 
        return super.onOptionsItemSelected(item); 

Step 3: Explanation of Code in

To get information about MainActivity and Layout in an Android App please refer:

Let’s go through above code step by step. First we make handle of TextView which is used to display output text
on screen.GestureDetectorCompat detects all gestures and events using event supplied by MotionEvent. Moreover,GestureDetector.OnGestureListener will notify user about a particular event and GestureDetector.OnDoubleTapListener will notify a Double tap on screen.

We pass GestureDetector.OnGestureListener and GestureDetector.OnDoubleTapListener as interface which are a collection of methods that we can override to include some more functionality. Interfaces in java are same as abstract classes in C++.

We are supplying reference id to the output_text and creating object DetectMe. This outputText will be defined as an id incontent_main.xml. DetectMe.setOnDoubleTapListener(this) allow us to detect double taps.

Now we are overriding predefined functions onSingleTapConfirmed, onDoubleTap etc. to show text on screen whenever they are called. Return value is true so that it can be assured that event is handled properly.

Finally we need to override onTouchEvent that will glue together our whole code. This is the default method that will be called whenever a user touches screen. Right now it is checking only touch part. To include gesture we have to callDetectMe.onTouchEvent(event)before super.onTouchEvent(event).

Step 4: Code in Content_main.xml and Strings.xml

Please type following code in content_main.xml created in step-1:

Step 5: Running Android Code

Run the code (Shift+F10) .

So that’s it. Isn’t it very easy? We have gone through whole code while explaining each line.Congrats!!

Please write comments wherever you have problem or want to give some suggestions.

You can also give comments on my Blog :

Be the First to Share


    • Make It Modular: Student Design Challenge

      Make It Modular: Student Design Challenge
    • Stone Concrete Cement Contest

      Stone Concrete Cement Contest
    • Remote Control Contest

      Remote Control Contest


    Dhruvi Sheladiya
    Dhruvi Sheladiya

    Question 3 years ago on Step 2


    i want to know that is there any way to detect WIFI on/off using touch gesture.