Android: Two different events for single tap and long press/double tap?
Asked Answered
K

2

6

I am trying to develop a feature where a single tap of an item will call an Intent to go to another Activity, and a long press OR double tap of the item does something else, such as allow you to edit the text.

So far I am only able to get both to happen at the same time but not individually. Does anyone have any ideas?

public boolean onTouchEvent(MotionEvent e) {
    return gestureScanner.onTouchEvent(e);
}


public boolean onSingleTapConfirmed(MotionEvent e) { 
    Intent i = new Intent(getContext(), SecondClass.class);
    getContext().startActivity(i);

    return true; 
}

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { return false; }
public void onLongPress(MotionEvent e) {
    Toast.makeText(getContext(), "Edit feature here", Toast.LENGTH_SHORT).show();

}
Kindliness answered 10/11, 2012 at 17:22 Comment(1)
Postsomecode, so i can rectify itWeald
K
7

I managed to solve the problem. It turned out all I needed to do is change the return value from false to true in the onDown() handler.

public boolean onTouchEvent(MotionEvent e) {
    return gestureScanner.onTouchEvent(e);
}

public boolean onSingleTapConfirmed(MotionEvent e) { 

    Intent i = new Intent(getContext(), SecondClass.class);
    getContext().startActivity(i);

    return true; 
}

public boolean onDown(MotionEvent e) { return true; }


public void onLongPress(MotionEvent e) {
    Toast.makeText(getContext(), "Edit Feature", Toast.LENGTH_SHORT).show();

}
Kindliness answered 10/11, 2012 at 17:55 Comment(1)
Worked for me as well.Orson
S
5

Use a GestureDetector, the SimpleOnGestureListener has the methods that you want with onSingleTapConfirmed(), onLongPress(), and onDoubleTap().

Sudra answered 10/11, 2012 at 17:29 Comment(2)
I have been using a GestureDetector. If I put my Intent in the onSingleTapConfirmed() method it doesn't do anything. It just makes the onLongPress() work on a single tap :Kindliness
What type of View are you adding this to? It looks like you want a TextView that can become an EditText on a long-click.Sudra

© 2022 - 2024 — McMap. All rights reserved.