Saturday, February 20, 2016

Implement Gestures in Android Phone Android Studio

Hello everyone today I will be showing you how to capture touch events in your Android application

For detecting gesture you will need to import few classes in your code so import the following classes


1:  import android.view.MotionEvent;  
2:  import android.view.GestureDetector;  
3:  import android.support.v4.view.GestureDetectorCompat;  

You will also need to implement the following Interfaces

1. GestureDetector.OnGestureListener

2. GestureDetector.OnDoubleTapListener

Like this

1:  public class MainActivity extends AppCompatActivity implements GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener {  
Now we will need to implement all methods for these interfaces so press Alt + Insert this will list all methods available to be implemented Click on OK In you onCreate Method paste the following code

1:  txtans=(TextView) findViewById(R.id.txtans);  
2:      gestureDetector = new GestureDetectorCompat(this,this);  

Now for all the implemented method change the return type to true from false
We will also need to implement one last method so again press Alt + Insert and select onTouchEvent

1:    public boolean onTouchEvent(MotionEvent event) {  
2:      this.gestureDetector.onTouchEvent(event);  
3:      return super.onTouchEvent(event);  
4:    }  

I hope that it was informative for you and I would like to Thank you for reading.

No comments:

Post a Comment