Here is my code, I want to detect when my finger goes down the screen so when I touch the screen I detect the ACTION_DOWN
but when I go down the screen with my finger, ACTION_MOVE
is not recognized, neither ACTION_UP
Do you know why?
float x=0;
protected void onCreate(Bundle savedInstanceState) {
do things
ImageView image2 = (ImageView) findViewById(R.id.imageView3);
image2.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
if (arg1.getAction()==MotionEvent.ACTION_DOWN) {
x=arg1.getX();
}
else {
if (arg1.getAction()==MotionEvent.ACTION_MOVE){
if (arg1.getX()>x) {
do things
}
}
else {
if (arg1.getAction()==MotionEvent.ACTION_UP){
do things
}
}
}
}
If your
onTouch()
method returnsfalse
in response to the initialACTION_DOWN
MotionEvent
, it will not receive any of the subsequent events that belong to this particular gesture. Instead those touch events will be presented to the parent in the hierarchy.To phrase that another way, if you return
false
fromonTouch()
during the start of a gesture (theACTION_DOWN
), it signals that the method no longer wants to see any more of the gesture, and that the gesture's events should go to the parentView
.As markproxy points out in the comments below, returning
false
when theMotionEvent
is anything other than anACTION_DOWN
, such as anACTION_MOVE
for example, will not prevent subsequentMotionEvent
s in the current gesture being presented to theView
.Check return value of onTouch() method.
it should be true, not false.
hope it will works.
There are two cases:
1) If you also set OnClickListener() -> onTouch() should return false.
(If onTouch() returns true, OnClickListener() will not work)
2) If you do not set OnClickListener() -> onTouch() should return true.
(If onTouch() returns false, only ACTION_DOWN is called)
Couple of Things:
1) You need to return a boolean to show you're view consumed the event.
Here is a very simple implementation which works:
Here's the main.xml
::
MotionEvent.getAction()
returns more than just the flag. Try the following:You can also use
MotionEvent.getActionMasked()
. See also http://developer.android.com/reference/android/view/MotionEvent.html