There are some common gestures like tap, double tap, scroll, fling, long press, and other gestures which can be used with our android screen and let’s see how, you know, these gesture events work and we will see how we can call these function to, you know, record these gestures.
Plese follow the steps given below in order to use touch gestures in Android:
Step.1) First of all, in a blank project drag and drop long text.
Step.2) Now go to your java folder and, mainactivity.java file and add some libraries for gestures.
Step.3) Now once all these classes are imported, we will implement the gesture detector interface through our class. And once you add or implement these two interfaces in your class, you will be able to see some kind of squiggly red line which shows that this method or this interface has some kind of, you know, classes or functions which are not implemented. You will be able to see this red bulb and just click here or alt+enter and in here just click the first option which is implement methods.
Step.4) So after clicking implement methods there will be a box popup which will show you all the method related to this gesture detector on doubletaplistener, which is onsingletapconfirm, ondoubletapconfirm, ondoubletapevent so these are the three events that are included through ondoubletaplistener and some of the methods are included through ongesturelistener also which are ondown, onshowpress, onsingletap, onscroll, onlong, and onfling. These are all the gesture methods so each, you know, method is related to one gesture, So just click OK and you will be able to see all the gestures methods will be automatically included in your class.
Step.5) Now what we are going to do is first of all, we will declare these variables, one is our textview variable and another method is private gesturedetectorcompat which detects the gesture and then we will declare the instance of it, for example, gesturedetect okay, and you know, it will show error because we need to include this library so just click alt+enter and import the class.
Step.6) Now we will cast this textview first in our onCreate method and we will also create an instance of this gesturedetect instance, okay? So in here, so gesturedetect = new gesturedetectorcompat and this constructor takes two arguments and these arguments are, if you can just see here, these arguments are the context and ongesturelistener so both we will define as this, so the context is class itself, okay, and once again we will use this instance and we will set setdoubletaplistener to this gesturedetect and the context will be this for this also.
Step.7) Now what we are going to use the textview to show the method which is called. So for example, we add or we perform some gesture, for example doubletap or showpress or onscroll, you know, gesture, this method or these method will be called whenever we perform any gesture on our android screen. So what we want to do is, we want to print which method is called in this textview, so just copy this textview variable here and just go little bit down and for each method which is created by gesturelistener and ondoubletap listener, we will add one textview to each method.
Step.8) The same thing we are going to do for all the method so ondoubletap. So we have just added the settext method to the textview so that we can print the method called on the textview, okay? You can even call these motionevent method in your text settext view, okay? So for example, if we want to add the, motionevent for this, we can just add a concatenation operator e.tostring, And this will print the motionevent string here. You can add this to, you know, every method but, you know, it’s not necessary but you can do it to get the extra information about your method which is called.
Step.9) Now what we are going to do is, once we have added this textview.settext to all these methods, we can call or we just need to call one more method, okay, and that method is onTouchEvent so just before the declaration to all the method relating to gesturedetector and gesture ondoubletaplistener, all the methods are implemented here. Therefore add this on touch event method above all these method which are created by ondoubleclicklistener and ongesturedetector. So in here, go here and just press alt insert okay, so just press alt+insert and then you will be able to see some auto generation code options and we are going to choose override method from here and in here, there are so many methods and the method we are searching for is ontouchevent. So make sure that this filter is set which adjusts the alphabetical order so it will be easier to find this method. So in here, you just need to scroll down and search for ontouchevent which is this event and click OK.
This will create this onTouchEvent and inside this onTouchEvent, just before this return onTouchEvent, we are going to just use our gesturedetector variable or gesturedetector object which is an instance of gesturedetectorcompat inside this onTouchEvent, we going to call the gesturedetect.ontouchevent and just pass this event as an argument.
So now our app is running and you will see there is nothing inside our textview right now but once you scroll here or tap here or long tap here, all the method will be recorded. For example, scroll and you can see onscroll method is called and you can also see the details of this motion detection which is the coordinates here, the action performed, the time, event time and other features which are, you know, called by this event e.tostring.
Complete Code For Android Gestures
app-java-MainActivity.java
package com.example.programmingknowledge.gesturesapp;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.support.v4.view.GestureDetectorCompat;
import android.view.MotionEvent;
import android.gesture.Gesture;
import static android.view.GestureDetector.*;
public class MainActivity extends ActionBarActivity implements onGestureListener,onDoubleTapListener{
private static TextView textView;
private GestureDetectorCompat GestureDetect;
@Override
protected void class onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView);
GestureDetect = new GestureDetectorCompat(this,this);
GestureDetect.setDoubleTapListener(this);
}
@Override
public boolean onCreateOptionMenu(Menu menu) {
//inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if(id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onTouchEvent(MotionEvent event){
GestureDetect.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
textView.setText("onSingleTapConfirmed" + e.toString());
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
textView.setText("onDoubleTap" + e.toString());
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e){
textView.setText("onDoubleTapEvent" + e.toString());
return false;
}
@Override
public boolean onDown(MotionEvent e){
textView.setText("onDown" + e1.toString() + e2.toString());
return false;
}
@Override
public void onShowPress(MotionEvent e){
textView.setText("onShowPress" + e.toString());
}
@Override
public boolean onSingleTapUp(Motion e) {
textView.setText("onSingleTapUp" + e.toString());
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.setText("onScroll" + e1.toString() + e2.toString());
return false;
}
@Override
public void onLongPress(MotionEvent e) {
textView.setText("onLongPress" + e.toString());
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
textView.setText("onFling" + e1.toString());
return false;
}
}