Introduction

Android is one of the most popular operating systems for mobile. Nowadays, gestures are an important element of Android apps. Gestures make it easy to access the apps and make things convenient for the user.
In this article, I will show you how to implement gestures in Android applications using Android Studio. Android is a kernel-based operating system. In Android, users can modify the GUI components and source code.
Requirements

Steps to be followed

Carefully follow my steps to implement gestures in Android applications using Android Studio. I have included the source code too.
Step 1
Open Android Studio and start a new project.
Android
Step 2
Put the application name and company domain. If you wish to use C++ for coding this project, mark the "Include C++ support" checkbox and click Next.
Android
Step 3
Select the Android minimum SDK and click Next.
Android
Step 4
Choose "Basic Activity" and click Next.
Android
Step 5
Put the activity name and layout name. Android Studio basically takes Java class name as what you provide for activity name. Now, click Finish.
Android
Step 6
Go to activity_main.xml and click the text button. This xml file contains the designing code for the Android app. In the activity_main.xml file, copy and paste the below code.
Activity_main.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. tools:context="ganeshannt.gesturesample.MainActivity">
  8. <TextView
  9. android:id="@+id/ganimsg"
  10. android:layout_width="wrap_content"
  11. android:layout_height="wrap_content"
  12. android:layout_alignParentTop="true"
  13. android:layout_centerHorizontal="true"
  14. android:layout_marginTop="230dp"
  15. android:text="TextView"
  16. android:textAppearance="@style/TextAppearance.AppCompat.Body2" />
  17. </RelativeLayout>
User interface
Android
Step 7
In the MainActivity.java file, copy and paste the below code. Java is a back-end language for Android. Do not replace your package name otherwise, the app will not run.
MainActivity.java code
  1. package ganeshannt.gesturesample;
  2. import android.support.v7.app.ActionBarActivity;
  3. import android.os.Bundle;
  4. import android.view.Menu;
  5. import android.view.MenuItem;
  6. import android.widget.TextView;
  7. import android.view.MotionEvent;
  8. import android.view.GestureDetector;
  9. import android.support.v4.view.GestureDetectorCompat;
  10. public class MainActivity extends ActionBarActivity implements GestureDetector.OnGestureListener,
  11. GestureDetector.OnDoubleTapListener {
  12. private TextView ganeshMessage;
  13. private GestureDetectorCompat gestureDetector;
  14. @Override
  15. protected void onCreate(Bundle savedInstanceState) {
  16. super.onCreate(savedInstanceState);
  17. setContentView(R.layout.activity_main);
  18. ganeshMessage = (TextView) findViewById(R.id.ganimsg);
  19. this.gestureDetector = new GestureDetectorCompat(this, this);
  20. gestureDetector.setOnDoubleTapListener(this);
  21. }
  22. ///////// GESTURE METHODS //////////
  23. @Override
  24. public boolean onSingleTapConfirmed(MotionEvent e) {
  25. ganeshMessage.setText("onSingleTapConfirmed");
  26. return true;
  27. }
  28. @Override
  29. public boolean onDoubleTap(MotionEvent e) {
  30. ganeshMessage.setText("onDoubleTap");
  31. return true;
  32. }
  33. @Override
  34. public boolean onDoubleTapEvent(MotionEvent e) {
  35. ganeshMessage.setText("onDoubleTapEvent");
  36. return true;
  37. }
  38. @Override
  39. public boolean onDown(MotionEvent e) {
  40. ganeshMessage.setText("onDown");
  41. return true;
  42. }
  43. @Override
  44. public void onShowPress(MotionEvent e) {
  45. ganeshMessage.setText("onShowPress");
  46. }
  47. @Override
  48. public boolean onSingleTapUp(MotionEvent e) {
  49. ganeshMessage.setText("onSingleTapUp");
  50. return true;
  51. }
  52. @Override
  53. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  54. ganeshMessage.setText("onScroll");
  55. return true;
  56. }
  57. @Override
  58. public void onLongPress(MotionEvent e) {
  59. ganeshMessage.setText("onLongPress");
  60. }
  61. @Override
  62. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  63. ganeshMessage.setText("onFling");
  64. return true;
  65. }
  66. ///////// GESTURE METHODS //////////
  67. @Override
  68. public boolean onTouchEvent(MotionEvent event) {
  69. this.gestureDetector.onTouchEvent(event);
  70. return super.onTouchEvent(event);
  71. }
  72. @Override
  73. public boolean onCreateOptionsMenu(Menu menu) {
  74. // Inflate the menu; this adds items to the action bar if it is present.
  75. getMenuInflater().inflate(R.menu.menu_main, menu);
  76. return true;
  77. }
  78. @Override
  79. public boolean onOptionsItemSelected(MenuItem item) {
  80. // Handle action bar item clicks here. The action bar will
  81. // automatically handle clicks on the Home/Up button, so long
  82. // as you specify a parent activity in AndroidManifest.xml.
  83. int id = item.getItemId();
  84. //noinspection SimplifiableIfStatement
  85. if (id == R.id.action_settings) {
  86. return true;
  87. }
  88. return super.onOptionsItemSelected(item);
  89. }
  90. }
Step 8
As we need to perform each java activity change in AndroidManifest, add the below code in the AndroidManifest.xml file.
AndroidManifest.xml code
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  3. package="ganeshannt.gesturesample">
  4. <application
  5. android:allowBackup="true"
  6. android:icon="@mipmap/ic_launcher"
  7. android:label="@string/app_name"
  8. android:roundIcon="@mipmap/ic_launcher_round"
  9. android:supportsRtl="true"
  10. android:theme="@style/AppTheme">
  11. <activity android:name=".MainActivity">
  12. <intent-filter>
  13. <action android:name="android.intent.action.MAIN" />
  14. <category android:name="android.intent.category.LAUNCHER" />
  15. </intent-filter>
  16. </activity>
  17. </application>
  18. </manifest>
Step 9
This is our user interface of the application. Click the "Make Project" option.
Android
Step 10
Run the application >> select virtual machine >> click OK.
Android
Deliverables
Here, we have successfully implemented gestures in an Android application.
Android
Android
Android
Android
Don’t forget to like and follow me. If you have any doubts, just comment below.
Source code - https://github.com/GaneshanNT/GestureSample