Android: can't seem to use MotionEvent.ACTION_MOVE correctly
Asked Answered
E

4

8

I am quite new to Android programming and Java (though I have some experience with C#, so that helps).

At this moment I'm goofing around with a couple of things to get to know how everything works. I've made an activity which implements onTouchListener. I've overridden onTouch and have a switch on event.getAction():

public boolean onTouch(View v, MotionEvent event) 
{
    float x; 
    float y;    

    switch (event.getAction())
    {
        case MotionEvent.ACTION_DOWN: // gets called
        {
            x = event.getX();
            y = event.getY();   
            circle c = new circle(this, x, y, 10, 0xFFFFFF);
            _main.addView(c, tapCount++);
            break;
        }
        case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
        {
            x = event.getX();
            y = event.getY();
            circle c = new circle(this, x, y, 10, 0xFFFFFF);
            _main.addView(c, tapCount++);
            break;
        }
    }
    return false;
}

Where "circle" is a class which draws a circle.

What I expected to see was a trail of circles as I dragged my finger over the screen. In stead, the circle is only being drawn when I start touching.

I have compared my code to examples (for example: this blogpost by Google: http://android-developers.blogspot.com/2010/06/making-sense-of-multitouch.html ) and I can't find my mistake.

EDIT: Link to the full class: http://pastebin.com/tVDQjQhu

EDIT: Fixed. One has to return true in the onTouch() function. d'oh!

Eighth answered 8/6, 2011 at 7:36 Comment(1)
why would you return false from onTouch?Proselyte
L
46

I was having the same problem while using MotionEvent.ACTION_MOVE. For MotionEvent.ACTION_MOVE to work, return true instead of false.

Lefton answered 23/6, 2011 at 17:17 Comment(2)
+1 Do you have any idea why this is how it works? Could you point us in the direction where this is documented, a place where there would be an explanation? Thanks!Volute
When you override onTouchEvent the boolean your return boolean indicates if the event was consumed or not.Ortolan
T
15

For me Solution was

return true

in the onTouch function as you should use this

public boolean onTouch(View v, MotionEvent event){
float x; 
float y;    

switch (event.getAction())
{
    case MotionEvent.ACTION_DOWN: // gets called
    {
        x = event.getX();
        y = event.getY();   
        circle c = new circle(this, x, y, 10, 0xFFFFFF);
        _main.addView(c, tapCount++);
        break;
    }
    case MotionEvent.ACTION_MOVE: // doesnt seem to do anything
    {
        x = event.getX();
        y = event.getY();
        circle c = new circle(this, x, y, 10, 0xFFFFFF);
        _main.addView(c, tapCount++);
        break;
    }
}
return true; //the problem was here

}

when you return false from onTouch(View v, MotionEvent event) then only MotionEvent.ACTION_DOWN will be called. so you should return true from this function

Hope this is helps

Taddeusz answered 17/7, 2012 at 6:2 Comment(0)
S
1

Had the same problems too, I don't know if it's a bug or not, but I managed to make it work by adding OnClickListener to the implementation, and implementing public void onClick(View v) then also adding setOnClickListener(this) on the constructor.

Sosthenna answered 14/2, 2012 at 15:30 Comment(0)
G
0

You have to use return true for every event. If you use return only one time then only Action_Down will call. This will help you.

@Override
    public boolean onTouchEvent(MotionEvent event) {
        final float X = event.getX();
        final float Y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_MOVE:
                x = X;
                y = Y;
                invalidate();
                return true;
            case MotionEvent.ACTION_UP:
                invalidate();
                return true;
        }
        return super.onTouchEvent(event);
    }
Guitarist answered 16/3, 2021 at 6:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.