GestureDetector and GestureDetector.OnGestureListener

Tutorials with advanced 'difficulty' and more Lines of Code.

GestureDetector and GestureDetector.OnGestureListener

Postby Danuubz » Tue Oct 21, 2008 7:36 am

Here's a small example showing how GestureDetector and GestureDetector.OnGestureListener works. They can be used to recognize gestures like:

DOWN, FLING, LONG_PRESS, SCROLL, SHOW_PRESS and SINGLE_TAP_UP

I've implemented a small TextView that shows which gesture is made actually on screen.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package de.scrolltest;
  3.  
  4.  
  5.  
  6. import android.app.Activity;
  7.  
  8. import android.os.Bundle;
  9.  
  10. import android.widget.LinearLayout;
  11.  
  12. import android.view.MotionEvent;
  13.  
  14. import android.view.GestureDetector.OnGestureListener;
  15.  
  16. import android.view.GestureDetector;
  17.  
  18. import android.widget.TextView;
  19.  
  20. import android.graphics.Color;
  21.  
  22.  
  23.  
  24. public class ScrollTest extends Activity implements OnGestureListener
  25.  
  26. {    
  27.  
  28.     private LinearLayout main;    
  29.  
  30.     private TextView viewA;
  31.  
  32.    
  33.  
  34.     private GestureDetector gestureScanner;
  35.  
  36.        
  37.  
  38.     @Override
  39.  
  40.     public void onCreate(Bundle savedInstanceState)
  41.  
  42.     {
  43.  
  44.         super.onCreate(savedInstanceState);
  45.  
  46.        
  47.  
  48.         gestureScanner = new GestureDetector(this);
  49.  
  50.        
  51.  
  52.         main = new LinearLayout(this);
  53.  
  54.         main.setBackgroundColor(Color.GRAY);
  55.  
  56.         main.setLayoutParams(new LinearLayout.LayoutParams(320,480));
  57.  
  58.        
  59.  
  60.         viewA = new TextView(this);
  61.  
  62.         viewA.setBackgroundColor(Color.YELLOW);
  63.  
  64.         viewA.setTextColor(Color.BLACK);
  65.  
  66.         viewA.setTextSize(16);
  67.  
  68.         viewA.setLayoutParams(new LinearLayout.LayoutParams(320,80));
  69.  
  70.         main.addView(viewA);
  71.  
  72.        
  73.  
  74.         setContentView(main);
  75.  
  76.     }
  77.  
  78.    
  79.  
  80.     @Override
  81.  
  82.     public boolean onTouchEvent(MotionEvent me)
  83.  
  84.     {
  85.  
  86.         return gestureScanner.onTouchEvent(me);
  87.  
  88.     }
  89.  
  90.    
  91.  
  92.     @Override
  93.  
  94.     public boolean onDown(MotionEvent e)
  95.  
  96.     {
  97.  
  98.         viewA.setText("-" + "DOWN" + "-");
  99.  
  100.         return true;
  101.  
  102.     }
  103.  
  104.    
  105.  
  106.     @Override
  107.  
  108.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
  109.  
  110.     {
  111.  
  112.         viewA.setText("-" + "FLING" + "-");
  113.  
  114.         return true;
  115.  
  116.     }
  117.  
  118.    
  119.  
  120.     @Override
  121.  
  122.     public void onLongPress(MotionEvent e)
  123.  
  124.     {
  125.  
  126.         viewA.setText("-" + "LONG PRESS" + "-");
  127.  
  128.     }
  129.  
  130.    
  131.  
  132.     @Override
  133.  
  134.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  135.  
  136.     {
  137.  
  138.         viewA.setText("-" + "SCROLL" + "-");
  139.  
  140.         return true;
  141.  
  142.     }
  143.  
  144.    
  145.  
  146.     @Override
  147.  
  148.     public void onShowPress(MotionEvent e)
  149.  
  150.     {
  151.  
  152.         viewA.setText("-" + "SHOW PRESS" + "-");
  153.  
  154.     }    
  155.  
  156.    
  157.  
  158.     @Override
  159.  
  160.     public boolean onSingleTapUp(MotionEvent e)    
  161.  
  162.     {
  163.  
  164.         viewA.setText("-" + "SINGLE TAP UP" + "-");
  165.  
  166.         return true;
  167.  
  168.     }
  169.  
  170. }
  171.  
  172.  
Parsed in 0.040 seconds, using GeSHi 1.0.8.4
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Top

would I be able to apply your fling tutrial to list itmes

Postby mikpg » Tue Nov 18, 2008 2:34 pm

I would like to know how I could use the same gesture recognition on list items?
Mikpg
mikpg
Freshman
Freshman
 
Posts: 5
Joined: Tue Nov 11, 2008 4:02 pm
Location: null

Postby Danuubz » Thu Nov 20, 2008 1:24 am

You can do it by using inner class list items that extend a view or widget class like:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. private int activeId;
  3.  
  4. private GestureDetector gestureScanner;
  5.  
  6.  
  7.  
  8. ...
  9.  
  10.  
  11.  
  12. class MyListItem extends View
  13.  
  14. {
  15.  
  16.     private int id;
  17.  
  18.  
  19.  
  20.     public MyListItem(Context context, int id)
  21.  
  22.     {
  23.  
  24.          super(context);
  25.  
  26.          this.id = id;
  27.  
  28.     }
  29.  
  30.  
  31.  
  32.     @Override
  33.  
  34.     public boolean onTouchEvent(MotionEvent me)
  35.  
  36.     {
  37.  
  38.          activeId = this.id;
  39.  
  40.          return gestureScanner.onTouchEvent(me);
  41.  
  42.     }
  43.  
  44. }
  45.  
  46.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Now if you touch the list item, the GestureDetector class invokes the particular listener method of the class that implements the OnGestureListener interface.
If you take this way, it is necessary to handle the id manually in the listener methods, so that it recognizes which item was touched by the user. (If you would like to make it with XML, i cannot help, as I always do each GUI part programmatically).
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

directional

Postby pentace » Fri Dec 12, 2008 7:57 am

Would you be able to show an example of how to determine if the fling event or scroll event went in the left or right direction?
pentace
Junior Developer
Junior Developer
 
Posts: 19
Joined: Wed Oct 22, 2008 2:16 pm

Postby Danuubz » Fri Dec 12, 2008 2:37 pm

Yes.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. @Override
  2.  
  3.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
  4.  
  5.     {
  6.  
  7.         return true;
  8.  
  9.     }
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


If distanceX > 0: scroll in right direction
If distanceX < 0: scroll in left direction

If distanceY > 0: scroll down
If distanceY < 0: scroll up
Sticky Note Cork&Orc [Android Market] : http://www.youtube.com/watch?v=ewWaquAwZQY
User avatar
Danuubz
Experienced Developer
Experienced Developer
 
Posts: 78
Joined: Wed Dec 19, 2007 10:44 pm
Location: Germany

Postby guruk » Tue Mar 31, 2009 12:49 pm

Great Example,
helped me a lot.

Now I have a question.

In my Layout is in the top a textview and bellow many buttons (calendar)

Just in the textview the recognition is great, but over my 31 Buttons nothing happen.

Also when I would do it like in your example for the listview.. how would it work over
31 buttons :) would it only measure in the small range (20px) of a field or over the hole screen.

That is what I would like to realize. In the top a textview, bellow 31 buttons but a
gesture recognition over the hole screen.

Any Ideas?

A small example over 3 Buttons would alread help a lot.

Thanks
Chris
guruk
Junior Developer
Junior Developer
 
Posts: 24
Joined: Sun Mar 08, 2009 8:05 pm

Top

Postby ajax » Sat Apr 17, 2010 4:59 pm

guruk wrote:Great Example,
helped me a lot.

Now I have a question.

In my Layout is in the top a textview and bellow many buttons (calendar)

Just in the textview the recognition is great, but over my 31 Buttons nothing happen.

Also when I would do it like in your example for the listview.. how would it work over
31 buttons :) would it only measure in the small range (20px) of a field or over the hole screen.

That is what I would like to realize. In the top a textview, bellow 31 buttons but a
gesture recognition over the hole screen.

Any Ideas?

A small example over 3 Buttons would alread help a lot.

Thanks
Chris



I have the same problem mate
Anyone can help?
ajax
Once Poster
Once Poster
 
Posts: 1
Joined: Sat Apr 17, 2010 4:20 pm

Postby BenRose3d » Thu May 13, 2010 8:00 pm

I started out adding buttons to give you an example, and in my testing I noticed the emulator wasn't registering all of the gestures. The problem gestures were the ones that were just vertical or steep diagonals. Gesture moving left to right was registered, as well as circles, boxes, etc. while dragging over buttons.

I was messing around with the example here:
http://android-developers.blogspot.com/ ... rs+Blog%29


Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4.  
  5.  
  6. <RelativeLayout android:id="@+id/RelativeLayout01"
  7.  
  8. android:layout_width="fill_parent"
  9.  
  10. android:layout_height="fill_parent"
  11.  
  12. xmlns:android="http://schemas.android.com/apk/res/android">
  13.  
  14.  
  15.  
  16. <android.gesture.GestureOverlayView
  17.  
  18.         android:id="@+id/TestGestureOverlay"
  19.  
  20.         android:layout_width="fill_parent"
  21.  
  22.         android:layout_height="fill_parent">
  23.  
  24.        
  25.  
  26. <RelativeLayout android:id="@+id/RelativeLayoutInner"
  27.  
  28. android:layout_width="fill_parent"
  29.  
  30. android:layout_height="fill_parent"
  31.  
  32. xmlns:android="http://schemas.android.com/apk/res/android">    
  33.  
  34.        
  35.  
  36. <Button
  37.  
  38.         android:id="@+id/doneButton"
  39.  
  40.         android:layout_width="wrap_content"
  41.  
  42.         android:layout_height="wrap_content"
  43.  
  44.         android:layout_alignParentRight="true"
  45.  
  46.         android:layout_alignParentBottom="true"
  47.  
  48.         android:onClick="exitActivity"
  49.  
  50.         android:text="Done"/>
  51.  
  52.        
  53.  
  54.         <Button
  55.  
  56.         android:id="@+id/centerButton"
  57.  
  58.         android:layout_width="wrap_content"
  59.  
  60.         android:layout_height="wrap_content"
  61.  
  62.         android:layout_centerInParent="true"
  63.  
  64.         android:onClick="exitActivity"
  65.  
  66.         android:text="Done"/>
  67.  
  68.        
  69.  
  70.         <Button
  71.  
  72.         android:id="@+id/belowCenterButton"
  73.  
  74.         android:layout_width="wrap_content"
  75.  
  76.         android:layout_height="wrap_content"
  77.  
  78.         android:layout_below="@+id/centerButton"
  79.  
  80.         android:layout_centerHorizontal="true"
  81.  
  82.         android:onClick="exitActivity"
  83.  
  84.         android:text="Done"/>
  85.  
  86.        
  87.  
  88.         <Button
  89.  
  90.         android:id="@+id/aboveCenterButton"
  91.  
  92.         android:layout_width="wrap_content"
  93.  
  94.         android:layout_height="wrap_content"
  95.  
  96.         android:layout_above="@+id/centerButton"
  97.  
  98.         android:layout_centerHorizontal="true"
  99.  
  100.         android:onClick="exitActivity"
  101.  
  102.         android:text="Done"/>
  103.  
  104.        
  105.  
  106.         <Button
  107.  
  108.         android:id="@+id/leftOfCenterButton"
  109.  
  110.         android:layout_width="wrap_content"
  111.  
  112.         android:layout_height="wrap_content"
  113.  
  114.         android:layout_toLeftOf="@+id/centerButton"
  115.  
  116.         android:layout_centerVertical="true"
  117.  
  118.         android:onClick="exitActivity"
  119.  
  120.         android:text="Done"/>
  121.  
  122.        
  123.  
  124.         <Button
  125.  
  126.         android:id="@+id/rightOfCenterButton"
  127.  
  128.         android:layout_width="wrap_content"
  129.  
  130.         android:layout_height="wrap_content"
  131.  
  132.         android:layout_toRightOf="@+id/centerButton"
  133.  
  134.         android:layout_centerVertical="true"
  135.  
  136.         android:onClick="exitActivity"
  137.  
  138.         android:text="Done"/>
  139.  
  140.        
  141.  
  142.         </RelativeLayout>
  143.  
  144.        
  145.  
  146. </android.gesture.GestureOverlayView>  
  147.  
  148.        
  149.  
  150.  
  151.  
  152. </RelativeLayout>
  153.  
  154.  
Parsed in 0.008 seconds, using GeSHi 1.0.8.4


Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. package com.android.gesture.builder;
  3.  
  4.  
  5.  
  6.  
  7.  
  8. import java.util.ArrayList;
  9.  
  10. import android.app.Activity;
  11.  
  12. import android.gesture.Gesture;
  13.  
  14. import android.gesture.GestureLibrary;
  15.  
  16. import android.gesture.GestureOverlayView;
  17.  
  18. import android.gesture.Prediction;
  19.  
  20. import android.gesture.GestureOverlayView.OnGesturePerformedListener;
  21.  
  22. import android.os.Bundle;
  23.  
  24. import android.view.View;
  25.  
  26. import android.widget.Toast;
  27.  
  28.  
  29.  
  30. public class TestGesturesActivity extends Activity implements OnGesturePerformedListener {
  31.  
  32.  
  33.  
  34.        
  35.  
  36.         @Override
  37.  
  38.     protected void onCreate(Bundle savedInstanceState) {
  39.  
  40.         super.onCreate(savedInstanceState);
  41.  
  42.        
  43.  
  44.         setContentView(R.layout.test_gestures);
  45.  
  46.  
  47.  
  48.         GestureOverlayView overlay = (GestureOverlayView) findViewById(R.id.TestGestureOverlay);
  49.  
  50.         overlay.addOnGesturePerformedListener(this);
  51.  
  52.     }
  53.  
  54.        
  55.  
  56.         public void exitActivity(View V){
  57.  
  58.                 finish();
  59.  
  60.         }
  61.  
  62.        
  63.  
  64.         @Override
  65.  
  66.         public void onGesturePerformed(GestureOverlayView overlay, Gesture gesture) {
  67.  
  68.        
  69.  
  70.                 final GestureLibrary gestures = GestureBuilderActivity.getStore();
  71.  
  72.        
  73.  
  74.         ArrayList<Prediction> predictions = gestures.recognize(gesture);
  75.  
  76.  
  77.  
  78.         // We want at least one prediction
  79.  
  80.         if (predictions.size() > 0) {
  81.  
  82.             Prediction prediction = predictions.get(0);
  83.  
  84.             // We want at least some confidence in the result
  85.  
  86.             if (prediction.score > 1.0) {
  87.  
  88.                 // Show the spell
  89.  
  90.                 Toast.makeText(this, prediction.name, Toast.LENGTH_SHORT).show();
  91.  
  92.             } else {
  93.  
  94.                 Toast.makeText(this, "predictions loaded, but no good match", Toast.LENGTH_SHORT).show();
  95.  
  96.             }
  97.  
  98.         } else {
  99.  
  100.                 Toast.makeText(this, "no predictions loaded", Toast.LENGTH_SHORT).show();
  101.  
  102.         }
  103.  
  104.     }
  105.  
  106.        
  107.  
  108. }
  109.  
  110.  
  111.  
  112.  
Parsed in 0.043 seconds, using GeSHi 1.0.8.4
BenRose3d
Freshman
Freshman
 
Posts: 3
Joined: Mon Apr 05, 2010 10:12 pm

Re: GestureDetector and GestureDetector.OnGestureListener

Postby jrc.src » Wed Mar 23, 2011 11:32 am

Hello,

I am trying to implement swipe right and swipe left on my activity. My activity has full screen imageview and buttons(next/prev) on top of it using <merge> in xml.

My activity already has onTouchEvent to hide/show buttons on it. So when touched on Activity ..they are shown or hidden.

Now i want to implement swipe left/right, my onFling is not called.

Please guide me is it possible to have ontouchevent and onfling in acitivty togather.

::::EDIT:::::
I need to override onDown method to return true. It is working now. (updated in case it can help others) Thanks

Thanks in advance.
JRC
jrc.src
Freshman
Freshman
 
Posts: 5
Joined: Wed Sep 29, 2010 11:09 am

Top

Return to Advanced Tutorials

Who is online

Users browsing this forum: No registered users and 3 guests