Search code examples
javaandroidgesturedetector

Get double tapped/touched View using a single Gesture Detector with multiple Views connected


Im using a single Gesture Detector with multiple Views attached. I'm not sure if this is the best way to do it, but giving each view its own Gesture Detector makes my code very big. Now, I want to check which View is double tapped if I double tap on one of my Views. Can't figure out how :(. The reason I connect all Views to the same Gesture Detector is because they all need to do the same when double tapped.

I've searched the internet but can't find anything helpfull. I hope my code makes everything more clear.

implements View.OnTouchListener, GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener

gestureDetector = new GestureDetector(this, this);

textView1.setOnTouchListener(this);
textView2.setOnTouchListener(this);
textView3.setOnTouchListener(this);
textView4.setOnTouchListener(this);
textView5.setOnTouchListener(this);
textView6.setOnTouchListener(this);

@Override
public boolean onTouch(View v, MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

public boolean onDoubleTap(MotionEvent e) {
    editMode();
    //Get tapped View
    return false;
}

I would like to see an output inside onDoubleTap(); like: current double tapped View is: (one of my textView's). I also found out that if I tap textView1 first and then textView2, onDoubleTap(); will be triggerd. This means that it would be nice if the answer selects the View that's been tapped as last. Hope anyone can help :D


Solution

  • Okey i feel stupid right now but i found the answer. I just need to create a variable and set it in the OnTouch().

    @Override
    public boolean onTouch(View v, MotionEvent event) {
       //Get id of touched View
       getViewByID = v.getId();
       return gestureDetector.onTouchEvent(event);
    }
    

    Then I can use this variable whereever i want.

    if (getViewByID == R.id...) {
       //Change text for example
    }