Android - Simple Touch Gesture

Submitted by: 
Operating System: 
Visitors have accessed this post 845 times.

In this tutorial we will try to create a Simple Touch Gesture using Android. This is simple application can be used to any apps that needed to detect touch movement to call any functions. Android is a mobile operating system developed by Google. It used in several gadget like smartphone, tablet, and even television. Android is open source to developers who has an interest in developing mobile apps. It also provide an adaptive framework that allow the developer to develop an apps in a simpler way. So now let's do the coding.....

Getting Started:

First you will have to download & install the Android Development IDE (Android Studio or Eclipse). Android Studio is an open source development feel free to develop your things.

Here's the link for the Android Studio https://developer.android.com/studio/index.html.

Layout Design

We will now create the design for the application, first locate the layout file called activity_main.xml, this is the default name when create a new activity. Then write these codes inside your layout file.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> xmlns:app="<a href="http://schemas.android.com/apk/res-auto"
  5. " rel="nofollow">http://schemas.android.com/apk/res-auto"
  6. </a> xmlns:tools="<a href="http://schemas.android.com/tools"
  7. " rel="nofollow">http://schemas.android.com/tools"
  8. </a> android:layout_width="match_parent"
  9. android:layout_height="match_parent"
  10. tools:context="com.razormist.simpletouchgestures.MainActivity">
  11.  
  12. <TextView
  13. android:id="@+id/tv_title"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:text="Touch Gesture"
  17. android:layout_centerHorizontal="true"
  18. android:textSize="50sp"
  19. android:layout_marginTop="30dp"
  20. android:textColor="#000"/>
  21.  
  22. <TextView
  23. android:id="@+id/tv_result"
  24. android:layout_width="wrap_content"
  25. android:layout_height="wrap_content"
  26. android:text="Result Here..."
  27. android:layout_centerInParent="true"
  28. android:textSize="30dp"/>
  29.  
  30. </RelativeLayout>

Android Manifest File

The Android Manifest file provides essential information about your app to the Android system in which the system must required before running the code. It describe the overall information about the application. It contains some libraries that needed to access the several method within the app.

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <manifest xmlns:android="<a href="http://schemas.android.com/apk/res/android"
  3. " rel="nofollow">http://schemas.android.com/apk/res/android"
  4. </a> package="com.razormist.simpletouchgestures">
  5.  
  6. <application
  7. android:allowBackup="true"
  8. android:icon="@mipmap/ic_launcher"
  9. android:label="@string/app_name"
  10. android:roundIcon="@mipmap/ic_launcher_round"
  11. android:supportsRtl="true"
  12. android:theme="@style/AppTheme">
  13. <activity android:name=".MainActivity"
  14. android:configChanges="orientation"
  15. android:screenOrientation="portrait">
  16. <intent-filter>
  17. <action android:name="android.intent.action.MAIN" />
  18.  
  19. <category android:name="android.intent.category.LAUNCHER" />
  20. </intent-filter>
  21. </activity>
  22. </application>
  23. </manifest>

The Main Function

This code contains the main function of the application. This code will detect any touch method within the class. To start with first locate your MainActivity java file and open it, then then add this remote services by casting implements in the MainActivity

  1. implements OnGestureListener, OnDoubleTapListener

After that write this variable inside the MainActivity class

  1. private static TextView tv_result;
  2. private GestureDetectorCompat gestureDetectorCompat;

Then write these several method inside the activity class to make the apps work properly.

  1. @Override
  2. public boolean onTouchEvent(MotionEvent event) {
  3. gestureDetectorCompat.onTouchEvent(event);
  4. return super.onTouchEvent(event);
  5. }
  6.  
  7. @Override
  8. public boolean onSingleTapConfirmed(MotionEvent e) {
  9. tv_result.setText("OnSingleTap");
  10. tv_result.setTextColor(Color.rgb(255, 0, 0));
  11. return false;
  12. }
  13.  
  14. @Override
  15. public boolean onDoubleTap(MotionEvent e) {
  16. tv_result.setText("OnDoubleTap");
  17. tv_result.setTextColor(Color.rgb(255, 0, 127));
  18. return false;
  19. }
  20.  
  21. @Override
  22. public boolean onDoubleTapEvent(MotionEvent e) {
  23. tv_result.setText("OnDoubleTapEvent");
  24. tv_result.setTextColor(Color.rgb(102, 0, 51));
  25. return false;
  26. }
  27.  
  28. @Override
  29. public boolean onDown(MotionEvent e) {
  30. tv_result.setText("OnDown");
  31. tv_result.setTextColor(Color.rgb(76, 153, 0));
  32. return false;
  33. }
  34.  
  35. @Override
  36. public void onShowPress(MotionEvent e) {
  37. tv_result.setText("OnShowPress");
  38. tv_result.setTextColor(Color.rgb(255, 255, 51));
  39. }
  40.  
  41. @Override
  42. public boolean onSingleTapUp(MotionEvent e) {
  43. tv_result.setText("OnSingleTapUp");
  44. tv_result.setTextColor(Color.rgb(255, 255, 0));
  45. return false;
  46. }
  47.  
  48. @Override
  49. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
  50. tv_result.setText("OnScroll");
  51. tv_result.setTextColor(Color.rgb(0, 0, 255));
  52. return false;
  53. }
  54.  
  55. @Override
  56. public void onLongPress(MotionEvent e) {
  57. tv_result.setText("OnLongPress");
  58. tv_result.setTextColor(Color.rgb(102, 0, 204));
  59. }
  60.  
  61. @Override
  62. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
  63. tv_result.setText("OnFling");
  64. tv_result.setTextColor(Color.rgb(102, 178, 255));
  65. return false;
  66. }

Finally, initialize all the methods inside the onCreate method to run the application.

  1. tv_result = (TextView)findViewById(R.id.tv_result);
  2. gestureDetectorCompat = new GestureDetectorCompat(this, this);
  3. gestureDetectorCompat.setOnDoubleTapListener(this);

Try to run the app and see if it worked.

There you have it we have created a Simple Touch Gesture using Android. I hope that this tutorial help you to what you are looking for. For more updates and tutorials just kindly visit this site. Enjoy Coding!!!


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.