Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ACTION_UP and ACTION_DOWN Called at the same time

Tags:

android

I am using touch events for input. MotionEvent.ACTION_DOWN and MotionEvent.ACTION_UP are both called during a press. It should only call ACTION_UP when my finger is lifted from the surface. They are called at the same time. See the following code and logcat output.

    public boolean onTouchEvent(MotionEvent e) {
    // MotionEvent reports input details from the touch screen
    // and other input controls. In this case, you are only
    // interested in events where the touch position changed.

    float x = e.getX();
    float y = e.getY();

    switch (e.getAction()) {
        case MotionEvent.ACTION_DOWN:
            mRenderer.scanButtonsDown(x, y);

            Log.i("Touch", "Action Down Case");

        case MotionEvent.ACTION_UP:
            mRenderer.scanButtonsUp(x, y);

            Log.i("Touch", "Action Up Case");

    }

    mPreviousX = x;
    mPreviousY = y;
    return true;
}

Here is the logcat output:

09-01 14:58:24.801    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Down Case
09-01 14:58:24.811    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case
09-01 14:58:24.961    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case

The previous logcat generates one down case and two up cases when the screen is pressed once. Now I will have an extended press.

09-01 14:58:37.113    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Down Case
09-01 14:58:37.123    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case
09-01 14:58:41.097    4683-4683/nf.co.av_club.opengl I/Touch﹕ Action Up Case

See the timestamps? Down and Up are called at the same time. It is called again once I actually remove my finger from tablet two seconds later. Does anyone have suggestions?

like image 937
Steve Webster Avatar asked Oct 30 '25 15:10

Steve Webster


1 Answers

You missed thebreak;at the end of each case. Do like the following.

switch (e.getAction()) {
    case MotionEvent.ACTION_DOWN:
        mRenderer.scanButtonsDown(x, y);
        Log.i("Touch", "Action Down Case");
        break; //add this line

    case MotionEvent.ACTION_UP:
        mRenderer.scanButtonsUp(x, y);
        Log.i("Touch", "Action Up Case");
        break; //and this line

}
like image 135
George Avatar answered Nov 01 '25 05:11

George



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!