{"id":36739,"date":"2016-05-02T11:00:34","date_gmt":"2016-05-02T08:00:34","guid":{"rendered":"http:\/\/examples.javacodegeeks.com\/?p=36739"},"modified":"2023-11-09T15:20:40","modified_gmt":"2023-11-09T13:20:40","slug":"android-multitouch-example","status":"publish","type":"post","link":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/","title":{"rendered":"Android Multitouch Example"},"content":{"rendered":"<p>A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android is the <a href=\"http:\/\/developer.android.com\/reference\/android\/view\/MotionEvent.html\"> MotionEvent<\/a> class. Motion events describe actions in a set of axis values. The actions are the states that occur when a finger is going down or up. The axis values describe the position of this event and other movement properties.<\/p>\n<p>There are single touch events and multi touch events. It depends on the screen of the device if it can handle and report multiple touch events. Multi-touch screens emit one movement trace for each finger, meaning for each pointer.<\/p>\n<p>Every pointer (every finger touch) has a unique id, that is assigned when it first touches the screen. A pointer id remains valid until the pointer eventually goes up.<\/p>\n<p>In order for our views to react to touch events in an Activity, an OnTouchListener should be registered for each of the views.<\/p>\n<p>So, In this example we are going to extend a FrameLayout and use the multi touch events in order to give this Layout some special behaviour. We are going to make a custom onTouchListener for this FrameLayout and override the <code>dispatchTouchEvent()<\/code>.<\/p>\n<p>For our example will use the following tools in a Windows 64-bit or an OS X platform:<\/p>\n<ul>\n<li>JDK 1.7<\/li>\n<li>Android Studio 1.3.2<\/li>\n<li>Android SDK 5.1<\/li>\n<\/ul>\n<p>Let\u2019s take a closer look:<br \/>\n<span data-sheets-value=\"{&quot;1&quot;:2,&quot;2&quot;:&quot;&quot;}\" data-sheets-userformat=\"{&quot;2&quot;:515,&quot;3&quot;:{&quot;1&quot;:0},&quot;4&quot;:{&quot;1&quot;:2,&quot;2&quot;:16777215},&quot;12&quot;:0}\">[ulp id=&#8217;Ja8Orb5oPKdShcXt&#8217;]<\/span><\/p>\n<h2>1. Create a New Android Studio Project<\/h2>\n<div class=\"tip\"><strong>Tip<\/strong><br \/>\nYou may skip project creation and jump directly to the <a href=\"#code\"><strong>beginning of the example<\/strong><\/a> below.<\/div>\n<p>Open Android Studio and choose \u201cStart a new Android Studio Project\u201d in the welcome screen.<\/p>\n<p><figure id=\"attachment_27646\" aria-describedby=\"caption-attachment-27646\" style=\"width: 797px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs1.png\"><img decoding=\"async\" class=\"size-full wp-image-27646\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs1.png\" alt=\"\u201cWelcome to Android Studio\u201d screen. Choose \u201cStart a new Android Studio Project\u201d.\" width=\"797\" height=\"601\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs1.png 797w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs1-300x226.png 300w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/a><figcaption id=\"caption-attachment-27646\" class=\"wp-caption-text\">\u201cWelcome to Android Studio\u201d screen. Choose \u201cStart a new Android Studio Project\u201d.<\/figcaption><\/figure><\/p>\n<p>Specify the name of the application, the project and the package.<\/p>\n<p><figure id=\"attachment_36740\" aria-describedby=\"caption-attachment-36740\" style=\"width: 797px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex.png\"><img decoding=\"async\" class=\"size-full wp-image-36740\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex.png\" alt=\"\u201cConfigure your new project\u201d screen. Add your application name and the projects package name.\" width=\"797\" height=\"519\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex.png 797w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex-300x195.png 300w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex-768x500.png 768w\" sizes=\"(max-width: 797px) 100vw, 797px\" \/><\/a><figcaption id=\"caption-attachment-36740\" class=\"wp-caption-text\">\u201cConfigure your new project\u201d screen. Add your application name and the projects package name.<\/figcaption><\/figure><\/p>\n<p>In the next window, select the form factors your app will run on.<\/p>\n<p><figure id=\"attachment_27648\" aria-describedby=\"caption-attachment-27648\" style=\"width: 800px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs3.png\"><img decoding=\"async\" class=\"size-full wp-image-27648\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs3.png\" alt=\"\u201cTarget Android Devices\u201d screen.\" width=\"800\" height=\"512\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs3.png 800w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioexs3-300x192.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><figcaption id=\"caption-attachment-27648\" class=\"wp-caption-text\">\u201cTarget Android Devices\u201d screen.<\/figcaption><\/figure><\/p>\n<p>In the next window you should choose to \u201cAdd an activity to Mobile\u201d. In our example, we will choose to create a project with no activity, so choose: \u201cAdd no activity\u201d.<\/p>\n<p><figure id=\"attachment_27650\" aria-describedby=\"caption-attachment-27650\" style=\"width: 800px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioex4.png\"><img decoding=\"async\" class=\"size-full wp-image-27650\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioex4.png\" alt=\"\u201cAdd an activity to Mobile\u201d. Choose: \u201cAdd no activity\u201d.\" width=\"800\" height=\"512\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioex4.png 800w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2015\/10\/androidstudioex4-300x192.png 300w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/a><figcaption id=\"caption-attachment-27650\" class=\"wp-caption-text\">\u201cAdd an activity to Mobile\u201d. Choose: \u201cAdd no activity\u201d.<\/figcaption><\/figure><\/p>\n<p>Now press finish, and our project has just been created.<\/p>\n<h2>2. Create the layout of the project<\/h2>\n<p>Add a new xml file inside <code>\/res\/layout folder<\/code>, with name <code>main_activity.xml<\/code>. We should have the<code>res\/layout\/main_activity.xml<\/code> file and paste the code below.<\/p>\n<p><em><span style=\"text-decoration: underline;\">main_activity.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\n&lt;LinearLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    android:layout_width=\"match_parent\"\n    android:layout_height=\"match_parent\"\n    android:background=\"#ffffff\"\n    android:orientation=\"vertical\"&gt;\n\n    &lt;android.support.v7.widget.Toolbar\n        android:id=\"@+id\/toolbar\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"58dp\"\n        android:background=\"?attr\/colorPrimary\"\n        android:minHeight=\"?attr\/actionBarSize\"\n        android:title=\"@string\/app_name\"&gt;&lt;\/android.support.v7.widget.Toolbar&gt;\n\n    &lt;com.javacodegeeks.androidmultitouchexample.TouchableFrameLayout\n        android:id=\"@+id\/touchable_frame\"\n        android:layout_width=\"match_parent\"\n        android:layout_height=\"match_parent\"&gt;\n\n        &lt;TextView\n            android:id=\"@+id\/status\"\n            android:layout_width=\"match_parent\"\n            android:layout_height=\"match_parent\"\n            android:gravity=\"center\"\n            android:text=\"@string\/noActionKey\"\n            android:textColor=\"#262626\"\n            android:textSize=\"30dp\" \/&gt;\n\n    &lt;\/com.javacodegeeks.androidmultitouchexample.TouchableFrameLayout&gt;\n&lt;\/LinearLayout&gt;\n<\/pre>\n<h2>3. Creating the source code of the TouchableFrameLayout FrameLayout<\/h2>\n<p>Add a new Java class inside <code>src\/com.javacodegeeks.androidmultitouchexample\/<\/code> so that we are going to have the <code>src\/com.javacodegeeks.androidmultitouchexample\/TouchableFrameLayout.java<\/code> file and paste the code below.<div style=\"display:inline-block; margin: 15px 0;\"> <div id=\"adngin-JavaCodeGeeks_incontent_video-0\" style=\"display:inline-block;\"><\/div> <\/div><\/p>\n<p><em><span style=\"text-decoration: underline;\">TouchableFrameLayout.java<\/span><\/em><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.androidmultitouchexample;\n\nimport android.content.Context;\nimport android.graphics.PointF;\nimport android.util.AttributeSet;\nimport android.util.SparseArray;\nimport android.view.MotionEvent;\nimport android.widget.FrameLayout;\n\npublic class TouchableFrameLayout extends FrameLayout {\n\n    private SparseArray mActivePointers = new SparseArray();\n\n    private OnTouchListener onTouchListener;\n\n    float lastXPosition;\n    float lastYPosition;\n    double lastdist = 0;\n\n    Context ctx;\n    private boolean onScaleMove = false;\n\n    public TouchableFrameLayout(Context context) {\n        super(context);\n        ctx = context;\n    }\n\n\n    public TouchableFrameLayout(Context context, AttributeSet attrs) {\n        super(context, attrs);\n        ctx = context;\n    }\n\n    public TouchableFrameLayout(Context context, AttributeSet attrs, int defStyle) {\n        super(context, attrs, defStyle);\n        ctx = context;\n    }\n\n    public void setTouchListener(OnTouchListener onTouchListener) {\n        this.onTouchListener = onTouchListener;\n    }\n\n    @Override\n    public boolean dispatchTouchEvent(final MotionEvent event) {\n        if (onTouchListener == null)\n            return false;\n\n        int pointerIndex = event.getActionIndex();\n        int pointerId = event.getPointerId(pointerIndex);\n\n        switch (event.getAction() &amp; MotionEvent.ACTION_MASK) {\n            case MotionEvent.ACTION_DOWN:\n\n                lastYPosition = event.getY();\n                lastXPosition = event.getX();\n                onTouchListener.onTouch();\n                return true;\n\n            case MotionEvent.ACTION_UP:\n                onScaleMove = false;\n                lastdist = 0;\n                onTouchListener.onRelease();\n                break;\n\n            case MotionEvent.ACTION_MOVE:\n                int diffY = (int) (event.getY() - lastYPosition);\n                int diffX = (int) (event.getX() - lastXPosition);\n\n                lastYPosition = event.getY();\n                lastXPosition = event.getX();\n\n                \/\/Check if the action was jitter\n                if (Math.abs(diffX) &gt; 4 || Math.abs(diffY) &gt; 4) {\n\n                    if (onScaleMove) {\n                        double dist = 0;\n\n                        if (event.getPointerCount() &gt;= 2) {\n                            dist = Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) + Math.pow(event.getY(0) - event.getY(1), 2));\n                        }\n\n                        if ((Math.abs(dist - lastdist) &gt; 10) &amp;&amp; (lastdist &gt; 0) &amp;&amp; (dist &gt; 0)) {\n                            if (dist &lt; lastdist) {\n                                onTouchListener.onPinchIn();\n                            } else if (dist == lastdist) {\n                                \/\/    onTouchListener.onPinchStable();\n                            } else {\n                                onTouchListener.onPinchOut();\n                            }\n                        } else {\n                            onTouchListener.onTwoFingersDrag();\n                        }\n\n                        lastdist = dist;\n                        return false;\n                    } else {\n                        onTouchListener.onMove();\n                    }\n\n                }\n                break;\n            case MotionEvent.ACTION_CANCEL: {\n                onScaleMove = false;\n                mActivePointers.remove(pointerId);\n                onTouchListener.onRelease();\n                break;\n            }\n            case MotionEvent.ACTION_POINTER_DOWN:\n                onScaleMove = true;\n                onTouchListener.onSecondFingerOnLayout();\n                PointF f = new PointF();\n                f.x = event.getX(pointerIndex);\n                f.y = event.getY(pointerIndex);\n                mActivePointers.put(pointerId, f);\n\n                return false;\n        }\n        return super.dispatchTouchEvent(event);\n    }\n\n\n    public interface OnTouchListener {\n        void onTouch();\n\n        void onRelease();\n\n        void onPinchIn();\n\n        void onPinchOut();\n\n        void onMove();\n\n        void onTwoFingersDrag();\n\n        void onSecondFingerOnLayout();\n    }\n}\n<\/pre>\n<p>Let&#8217;s see in detail the code above.<\/p>\n<p>We have made a custom OnTouchListener interface that our custom layout is going to implement.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">    public interface OnTouchListener {\n        void onTouch();\n\n        void onRelease();\n\n        void onPinchIn();\n\n        void onPinchOut();\n\n        void onMove();\n\n        void onTwoFingersDrag();\n\n        void onSecondFingerOnLayout();\n    }\n<\/pre>\n<pre class=\"brush:java; wrap-lines:false\">    public void setTouchListener(OnTouchListener onTouchListener) {\n        this.onTouchListener = onTouchListener;\n    }\n<\/pre>\n<p>So, our custom layout should control when to dispatch the events above. For this reason we have to override the<br \/>\n<code>dispatchTouchEvent(final MotionEvent event)<\/code>.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">case MotionEvent.ACTION_DOWN:\n                lastYPosition = event.getY();\n                lastXPosition = event.getX();\n                onTouchListener.onTouch();\n                return true;\n<\/pre>\n<p>In this snippet, the first pointer that touches the screen. With <code>event.getY();<\/code> and <code>event.getX();<\/code> we can get the co-ordinates of the X and Y axis of this event.[ulp id='Ja8Orb5oPKdShcXt']<\/p>\n<pre class=\"brush:java; wrap-lines:false\"> case MotionEvent.ACTION_POINTER_DOWN:\n                onScaleMove = true;\n                onTouchListener.onSecondFingerOnLayout();\n                PointF f = new PointF();\n                f.x = event.getX(pointerIndex);\n                f.y = event.getY(pointerIndex);\n                mActivePointers.put(pointerId, f);\n\n                return false;\n<\/pre>\n<p>With the code above, we can get the second pointer the first pointer co-ordinates of the X and Y axis.<\/p>\n<pre class=\"brush:java; wrap-lines:false\">if (event.getPointerCount() &gt;= 2) {\n                            dist = Math.sqrt(Math.pow(event.getX(0) - event.getX(1), 2) + Math.pow(event.getY(0) - event.getY(1), 2));\n                        }\n\n                        if ((Math.abs(dist - lastdist) &gt; 10) &amp;&amp; (lastdist &gt; 0) &amp;&amp; (dist &gt; 0)) {\n                            if (dist &lt; lastdist) {\n                                onTouchListener.onPinchIn();\n                            } else if (dist == lastdist) {\n                                \/\/    onTouchListener.onPinchStable();\n                            } else {\n                                onTouchListener.onPinchOut();\n                            }\n                        } else {\n                            onTouchListener.onTwoFingersDrag();\n                        }\n\n<\/pre>\n<p>And with this piece of code, we can get distance between the two finger pointers and figure out if its is a &#8220;pinch&#8221; meaning a scale between the two fingers, or a simple drag.<\/p>\n<h2>4. Creating the source code of the main AndroidMultitouchActivity Activity<\/h2>\n<p>Add a new Java class inside <code>src\/com.javacodegeeks.androidmultitouchexample\/<\/code> so that we are going to have the <code>src\/com.javacodegeeks.androidmultitouchexample\/AndroidMultitouchActivity.java<\/code> file and paste the code below.<\/p>\n<p><em><span style=\"text-decoration: underline;\">AndroidMultitouchActivity.java<\/span><\/em><\/p>\n<pre class=\"brush:java; wrap-lines:false\">package com.javacodegeeks.androidmultitouchexample;\n\nimport android.app.Activity;\nimport android.os.Bundle;\nimport android.support.v7.widget.Toolbar;\nimport android.widget.TextView;\n\npublic class AndroidMultitouchActivity extends Activity {\n\n    private Toolbar toolbar;\n\n    @Override\n    public void onCreate(Bundle savedInstanceState) {\n        super.onCreate(savedInstanceState);\n        setContentView(R.layout.main_activity);\n\n        toolbar = (Toolbar) findViewById(R.id.toolbar);\n        toolbar.setTitle(R.string.app_name);\n\n        final TextView status = (TextView) findViewById(R.id.status);\n\n        TouchableFrameLayout frame = (TouchableFrameLayout) findViewById(R.id.touchable_frame);\n        frame.setTouchListener(new TouchableFrameLayout.OnTouchListener() {\n            @Override\n            public void onTouch() {\n                status.setText(R.string.onTouchKey);\n            }\n\n            @Override\n            public void onRelease() {\n                status.setText(R.string.onReleaseKey);\n            }\n\n            @Override\n            public void onPinchIn() {\n                status.setText(R.string.onPinchInKey);\n            }\n\n            @Override\n            public void onPinchOut() {\n                status.setText(R.string.onPinchOutKey);\n            }\n\n            @Override\n            public void onMove() {\n                status.setText(R.string.onMoveKey);\n            }\n\n            @Override\n            public void onTwoFingersDrag() {\n                status.setText(R.string.onTwoFingersDragKey);\n            }\n\n            @Override\n            public void onSecondFingerOnLayout() {\n                status.setText(R.string.onSecondFingerOnLayout);\n            }\n\n        });\n    }\n}\n<\/pre>\n<h2>5. Create the strings.xml<\/h2>\n<p>Add a new xml file inside <code>\/res\/values folder<\/code>, with name <code>strings.xml<\/code>. We should have the<code>res\/values\/strings.xml<\/code> file and paste the code below.<\/p>\n<p><em><span style=\"text-decoration: underline;\">strings.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;resources&gt;\n    &lt;string name=\"app_name\"&gt;AndroidMultitouchExample&lt;\/string&gt;\n    &lt;string name=\"noActionKey\"&gt;Touch me!&lt;\/string&gt;\n    &lt;string name=\"onTouchKey\"&gt;On Touch&lt;\/string&gt;\n    &lt;string name=\"onMoveKey\"&gt;On Move&lt;\/string&gt;\n    &lt;string name=\"onReleaseKey\"&gt;On Release&lt;\/string&gt;\n    &lt;string name=\"onPinchInKey\"&gt;On Pinch In&lt;\/string&gt;\n    &lt;string name=\"onPinchOutKey\"&gt;On Pinch Out&lt;\/string&gt;\n    &lt;string name=\"onSecondFingerOnLayout\"&gt;On Second Finger Touch&lt;\/string&gt;\n    &lt;string name=\"onTwoFingersDragKey\"&gt;On Two Fingers Drag&lt;\/string&gt;\n&lt;\/resources\n&gt;\n<\/pre>\n<h2>6. Android Manifest<\/h2>\n<p>The AndroidManifest.xml of our project is simple and contains no special permissions:<\/p>\n<p><em><span style=\"text-decoration: underline;\">AndroidManifest.xml<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">&lt;manifest xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\n    package=\"com.javacodegeeks.androidmultitouchexample\"&gt;\n\n    &lt;application\n        android:allowBackup=\"true\"\n        android:icon=\"@mipmap\/ic_launcher\"\n        android:label=\"@string\/app_name\"\n        android:supportsRtl=\"true\"\n        android:theme=\"@style\/AppTheme\"&gt;\n        &lt;activity\n            android:name=\"com.javacodegeeks.androidmultitouchexample.AndroidMultitouchActivity\"\n            android:label=\"@string\/app_name\"&gt;\n\n            &lt;intent-filter&gt;\n                &lt;action android:name=\"android.intent.action.MAIN\" \/&gt;\n                &lt;category android:name=\"android.intent.category.LAUNCHER\" \/&gt;\n            &lt;\/intent-filter&gt;\n\n        &lt;\/activity&gt;\n    &lt;\/application&gt;\n\n&lt;\/manifest\n&gt;\n<\/pre>\n<h2>7. build.gradle<\/h2>\n<p>The build.gradle of our project is simple and contains no special permissions:<\/p>\n<p><em><span style=\"text-decoration: underline;\">build.gradle<\/span><\/em><\/p>\n<pre class=\"brush:xml; wrap-lines:false;\">apply plugin: 'com.android.application'\n\nandroid {\n    compileSdkVersion 23\n    buildToolsVersion \"23.0.2\"\n\n    defaultConfig {\n        applicationId \"com.javacodegeeks.androidmultitouchexample\"\n        minSdkVersion 14\n        targetSdkVersion 23\n        versionCode 1\n        versionName \"1.0\"\n    }\n    buildTypes {\n        release {\n            minifyEnabled false\n            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'\n        }\n    }\n}\n\ndependencies {\n    compile fileTree(dir: 'libs', include: ['*.jar'])\n    compile 'com.android.support:appcompat-v7:23.2.1'\n}\n<\/pre>\n<h2>8. Build, compile and run<\/h2>\n<p>When we build, compile and run our project, the main AndroidMultitouchExample should look like this:<\/p>\n<p><figure id=\"attachment_36748\" aria-describedby=\"caption-attachment-36748\" style=\"width: 400px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex2.png\"><img decoding=\"async\" class=\"size-full wp-image-36748\" src=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex2.png\" alt=\"This is how our application looks.\" width=\"400\" height=\"711\" srcset=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex2.png 400w, https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/androidmultitouchex2-169x300.png 169w\" sizes=\"(max-width: 400px) 100vw, 400px\" \/><\/a><figcaption id=\"caption-attachment-36748\" class=\"wp-caption-text\">This is how our application looks.<\/figcaption><\/figure><\/p>\n<h2>9. Download the Android Studio Project<\/h2>\n<p>This was an example of Android Multitouch Example.<\/p>\n<div class=\"download\"><strong>Download<br \/>\n<\/strong>You can download the full source code of this example here: <a href=\"http:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2016\/04\/AndroidMultitouchExample.zip\"><strong>AndroidMultitouchExample<\/strong><\/a><\/div>\n<p>If you want to check more about Touchable Layouts please follow, the <a href=\"https:\/\/github.com\/ChryssaAliferi\/Android-TouchableLayouts\"> Android-TouchableLayouts<\/a> github library.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android is the MotionEvent class. Motion events describe actions in a set of axis values. The actions are the states that occur when a finger is going down or up. The &hellip;<\/p>\n","protected":false},"author":14,"featured_media":1202,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[267,1466,1467],"class_list":["post-36739","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","tag-android-ui","tag-events","tag-widget"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v26.5 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Android Multitouch Example - Java Code Geeks<\/title>\n<meta name=\"description\" content=\"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Android Multitouch Example - Java Code Geeks\" \/>\n<meta property=\"og:description\" content=\"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android\" \/>\n<meta property=\"og:url\" content=\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\" \/>\n<meta property=\"og:site_name\" content=\"Examples Java Code Geeks\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/javacodegeeks\" \/>\n<meta property=\"article:published_time\" content=\"2016-05-02T08:00:34+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-11-09T13:20:40+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"150\" \/>\n\t<meta property=\"og:image:height\" content=\"150\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Chryssa Aliferi\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:site\" content=\"@javacodegeeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Chryssa Aliferi\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"5 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\"},\"author\":{\"name\":\"Chryssa Aliferi\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\"},\"headline\":\"Android Multitouch Example\",\"datePublished\":\"2016-05-02T08:00:34+00:00\",\"dateModified\":\"2023-11-09T13:20:40+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\"},\"wordCount\":745,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"keywords\":[\"Android UI\",\"Events\",\"widget\"],\"articleSection\":[\"Android\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\",\"name\":\"Android Multitouch Example - Java Code Geeks\",\"isPartOf\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"datePublished\":\"2016-05-02T08:00:34+00:00\",\"dateModified\":\"2023-11-09T13:20:40+00:00\",\"description\":\"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android\",\"breadcrumb\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg\",\"width\":150,\"height\":150},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/examples.javacodegeeks.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Android\",\"item\":\"https:\/\/examples.javacodegeeks.com\/category\/android\/\"},{\"@type\":\"ListItem\",\"position\":3,\"name\":\"Android Multitouch Example\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#website\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"name\":\"Java Code Geeks\",\"description\":\"Java Examples and Code Snippets\",\"publisher\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\"},\"alternateName\":\"JCG\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#organization\",\"name\":\"Exelixis Media P.C.\",\"url\":\"https:\/\/examples.javacodegeeks.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png\",\"width\":864,\"height\":246,\"caption\":\"Exelixis Media P.C.\"},\"image\":{\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/javacodegeeks\",\"https:\/\/x.com\/javacodegeeks\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332\",\"name\":\"Chryssa Aliferi\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"contentUrl\":\"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg\",\"caption\":\"Chryssa Aliferi\"},\"description\":\"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.\",\"sameAs\":[\"http:\/\/www.javacodegeeks.com\/\"],\"url\":\"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Android Multitouch Example - Java Code Geeks","description":"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/","og_locale":"en_US","og_type":"article","og_title":"Android Multitouch Example - Java Code Geeks","og_description":"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android","og_url":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/","og_site_name":"Examples Java Code Geeks","article_publisher":"https:\/\/www.facebook.com\/javacodegeeks","article_published_time":"2016-05-02T08:00:34+00:00","article_modified_time":"2023-11-09T13:20:40+00:00","og_image":[{"width":150,"height":150,"url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","type":"image\/jpeg"}],"author":"Chryssa Aliferi","twitter_card":"summary_large_image","twitter_creator":"@javacodegeeks","twitter_site":"@javacodegeeks","twitter_misc":{"Written by":"Chryssa Aliferi","Est. reading time":"5 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#article","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/"},"author":{"name":"Chryssa Aliferi","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332"},"headline":"Android Multitouch Example","datePublished":"2016-05-02T08:00:34+00:00","dateModified":"2023-11-09T13:20:40+00:00","mainEntityOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/"},"wordCount":745,"commentCount":0,"publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","keywords":["Android UI","Events","widget"],"articleSection":["Android"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/","url":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/","name":"Android Multitouch Example - Java Code Geeks","isPartOf":{"@id":"https:\/\/examples.javacodegeeks.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage"},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage"},"thumbnailUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","datePublished":"2016-05-02T08:00:34+00:00","dateModified":"2023-11-09T13:20:40+00:00","description":"A multi-touch gesture is when multiple pointers (fingers) touch the screen at the same time. The basic class for support touch and multitouch in Android","breadcrumb":{"@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#primaryimage","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2012\/12\/android-logo.jpg","width":150,"height":150},{"@type":"BreadcrumbList","@id":"https:\/\/examples.javacodegeeks.com\/android\/android-multitouch-example\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/examples.javacodegeeks.com\/"},{"@type":"ListItem","position":2,"name":"Android","item":"https:\/\/examples.javacodegeeks.com\/category\/android\/"},{"@type":"ListItem","position":3,"name":"Android Multitouch Example"}]},{"@type":"WebSite","@id":"https:\/\/examples.javacodegeeks.com\/#website","url":"https:\/\/examples.javacodegeeks.com\/","name":"Java Code Geeks","description":"Java Examples and Code Snippets","publisher":{"@id":"https:\/\/examples.javacodegeeks.com\/#organization"},"alternateName":"JCG","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/examples.javacodegeeks.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/examples.javacodegeeks.com\/#organization","name":"Exelixis Media P.C.","url":"https:\/\/examples.javacodegeeks.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2022\/06\/exelixis-logo.png","width":864,"height":246,"caption":"Exelixis Media P.C."},"image":{"@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/javacodegeeks","https:\/\/x.com\/javacodegeeks"]},{"@type":"Person","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/863144453b9fc15d4184d71833dcf332","name":"Chryssa Aliferi","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/examples.javacodegeeks.com\/#\/schema\/person\/image\/","url":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","contentUrl":"https:\/\/examples.javacodegeeks.com\/wp-content\/uploads\/2014\/03\/Chryssa-Aliferi-96x96.jpg","caption":"Chryssa Aliferi"},"description":"Chryssa is a Computer Science graduate from Athens University of Economic and Business. During her studies, Chryssa carried out a great variety of projects ranging from networking to software engineering. She is very keen on front end development especially on mobile technologies and web applications. She has worked as a junior Software Engineer in the telecommunications area and currently works as an Android Developer.","sameAs":["http:\/\/www.javacodegeeks.com\/"],"url":"https:\/\/examples.javacodegeeks.com\/author\/chryssa-aliferi\/"}]}},"_links":{"self":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36739","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/users\/14"}],"replies":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/comments?post=36739"}],"version-history":[{"count":0,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/posts\/36739\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media\/1202"}],"wp:attachment":[{"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/media?parent=36739"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/categories?post=36739"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/examples.javacodegeeks.com\/wp-json\/wp\/v2\/tags?post=36739"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}