How to listen Double Tap on Imageview

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

How to listen Double Tap on Imageview

Postby sannan » Fri May 15, 2009 5:36 am

Hi,
I am completely new to android development and want to know how to use OnDoubleTapListener with my application. Any code snippet can work better...

Thanks
sannan
Freshman
Freshman
 
Posts: 5
Joined: Fri May 15, 2009 5:32 am
Location: Hyderabad

Top

...

Postby guian » Tue Sep 08, 2009 10:46 am

I was wondering the same thing. So that's what I found :

the source code "ScrollTest" allows you to trigger all moves (like down/up/scroll/singleTap/longPress). In order to do that, you have to create a gestureDetector Object.

I guess we can easily add the double tap using :
gestureDetector.setOnDoubleTapListener(GestureDetector.OnDoubleTapListener onDoubleTapListener)

I'm gona try it then I tell you ;)



source code for 'ScrollTest' :


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

yep!

Postby guian » Tue Sep 08, 2009 11:01 am

I was right :
you only have to add the onDoubleTapListener to your gestureDetector Object:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.    gestureScanner = new GestureDetector(this);  //was only this,  add the following :
  2.  
  3.         gestureScanner.setOnDoubleTapListener(new OnDoubleTapListener(){
  4.  
  5.                         public boolean onDoubleTap(MotionEvent e) {
  6.  
  7.                                 viewA.setText("-" + "onDoubleTap" + "-");
  8.  
  9.                                 return false;
  10.  
  11.                         }
  12.  
  13.                         public boolean onDoubleTapEvent(MotionEvent e) {
  14.  
  15.                                 viewA.setText("-" + "onDoubleTapEvent" + "-");
  16.  
  17.                                 return false;
  18.  
  19.                         }
  20.  
  21.                         public boolean onSingleTapConfirmed(MotionEvent e) {
  22.  
  23.                                 viewA.setText("-" + "onSingleTapConfirmed" + "-");
  24.  
  25.                                 return false;
  26.  
  27.                         }
  28.  
  29.                
  30.  
  31.         });
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
guian
Developer
Developer
 
Posts: 35
Joined: Tue Nov 18, 2008 3:43 pm

Re: yep!

Postby saamyandroid » Mon Nov 30, 2009 9:54 am

Thanks for this info.

But This is not clear.

in Double Tap -

i need to know Left to Right Tap.
Also Right to Left Tap.

how can i get these Tap separately.

Thanks in advance,
saamyandroid
Experienced Developer
Experienced Developer
 
Posts: 84
Joined: Wed Sep 09, 2009 8:17 pm
Location: INDIA

use two single tap

Postby guian » Mon Nov 30, 2009 10:12 am

I guess if you don't implement the onDoubleTapListener

you can catch two singleTap (detect the time between those tap using System.getTimeInmillis() ) store those event in memory in order to compare them (if the timing is good to take them as one double tap)

I don't think you can compare the position of the two tap from a doubleTap event since a real double tap event occurs at one single position)

hmmm (reflexion)
...

the onDoubleTap event comes up with a motionEvent object
maybe you can play around with it :
getX() gives you the X position of the first pointer of this motionEvent

getX(int pointerIndex) gives you the x position of the pointer at the given index...
(so possibly the second tap position... )

maybe we have a better solution here but I'm not sure... can someone confirm this idea ?
guian
Developer
Developer
 
Posts: 35
Joined: Tue Nov 18, 2008 3:43 pm

Re: How to listen Double Tap on Imageview

Postby tannerresearch » Wed Aug 11, 2010 6:14 pm

From what I've gathered, this can only be done on Activities? I was really hoping to implement doubletaplistener on a custom view I made, but I can't seem to get it to work. Any interaction with the screen is interpreted as a long tap.
User avatar
tannerresearch
Junior Developer
Junior Developer
 
Posts: 21
Joined: Wed Jan 07, 2009 6:43 pm

Top

Re: How to listen Double Tap on Imageview

Postby pskink » Wed Aug 11, 2010 8:16 pm

tannerresearch wrote:From what I've gathered, this can only be done on Activities? I was really hoping to implement doubletaplistener on a custom view I made, but I can't seem to get it to work. Any interaction with the screen is interpreted as a long tap.


it doesn't matter if you implement doubletaplistener either on Activities or View level, it works the same
pskink
pskink
Master Developer
Master Developer
 
Posts: 719
Joined: Mon Nov 24, 2008 3:49 pm

Re: How to listen Double Tap on Imageview

Postby guian » Mon Aug 23, 2010 11:04 am

indeed, that's true.

your only need is to implement the interface GestureDetector.OnGestureListener.

So the constructor can recognise your current class as an OnGestureListener ;)
( new GestureDetector(this) )
guian
Developer
Developer
 
Posts: 35
Joined: Tue Nov 18, 2008 3:43 pm

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 4 guests