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 androidtutorialpoint.com and love to do programming in my free time. Visit my site http://www.androidtutorialpoint.com/ 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: http://www.androidtutorialpoint.com/basics/connect...

or at my instructables tutorial here:
https://www.instructables.com/id/Android-Tutorial-C...

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 application.example.com (I used my company domain i.e androidtutorialpoint.com. 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 MainActivity.java, 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 MainActivity.java

Please type following code in MainActivity.java created in step-1:

package com.androidtutorialpoint.myapplication; 
import android.os.Bundle; 
import android.support.design.widget.FloatingActionButton; 
import android.support.design.widget.Snackbar; 
import android.support.v4.view.GestureDetectorCompat; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.Toolbar; 
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.OnGestureListener, 
                                  GestureDetector.OnDoubleTapListener { 
 
    private TextView output_text;                 // This is added for Text output 
    private GestureDetectorCompat DetectMe;       
 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.activity_main); 
        output_text = (TextView) findViewById(R.id.outputText);  // Taking reference of text to be displayed on screen
        DetectMe = new GestureDetectorCompat(this,this); 
        DetectMe.setOnDoubleTapListener(this); 
 
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
        setSupportActionBar(toolbar); 
 
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab); 
        fab.setOnClickListener(new View.OnClickListener() { 
            @Override 
            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.
  
    @Override 
    public boolean onSingleTapConfirmed(MotionEvent e) { 
        output_text.setText("onSingleTapConfirmed"); 
        return true; 
    } 
 
    @Override 
    public boolean onDoubleTap(MotionEvent e) { 
        output_text.setText("onDoubleTap"); 
        return true; 
    } 
 
    @Override 
    public boolean onDoubleTapEvent(MotionEvent e) { 
        output_text.setText("onDoubleTapEvent"); 
        return true; 
    } 
 
    @Override 
    public boolean onDown(MotionEvent e) { 
        output_text.setText("onDown"); 
        return true; 
    } 
 
    @Override 
    public void onShowPress(MotionEvent e) { 
        output_text.setText("onShowPress"); 
 
    } 
 
    @Override 
    public boolean onSingleTapUp(MotionEvent e) { 
        output_text.setText("onSingleTapUp"); 
        return true; 
    } 
 
    @Override 
    public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) { 
        output_text.setText("onScroll"); 
        return true; 
    } 
 
    @Override 
    public void onLongPress(MotionEvent e) { 
        output_text.setText("onLongPress"); 
    } 
 
    @Override 
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { 
        output_text.setText("onFling"); 
        return true; 
    } 
 
    @Override 
    public boolean onTouchEvent(MotionEvent event){ 
        this.DetectMe.onTouchEvent(event); 
        return super.onTouchEvent(event); 
    } 
 
    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
        // Inflate the menu; this adds items to the action bar if it is present. 
        getMenuInflater().inflate(R.menu.menu_main, menu); 
        return true; 
    } 
 
    @Override 
    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 == R.id.action_settings) { 
            return true; 
        } 
 
        return super.onOptionsItemSelected(item); 
    } 
}

Step 3: Explanation of Code in MainActivity.java

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

http://www.androidtutorialpoint.com/basics/android...

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 : http://www.androidtutorialpoint.com/basics/androi...