Apoz
Apoz

Reputation: 513

Android Listener to detect when the screen is being pressed

I have a view in my Android app that should be updated constantly when the screen is being pressed. Most listeners I found only invalidate the view once when the user touches the screen for the first time, or when the user removes his hand from the screen.

Is there such a thing as a listener that is constantly triggered as long as the screen is touched ? Or is there any other way to do this ? I know, that sounds like something really simple, but I haven't found a way to do it.

Upvotes: 5

Views: 8906

Answers (1)

Raghav Sood
Raghav Sood

Reputation: 82543

Override onTouch() and set a flag in ACTION_DOWN. As long as ACTION_UP isn't called after that, the user is touching the screen.

boolean pressed = false;

@Override
    public boolean onTouch(View v, MotionEvent event) {

        //int action = event.getAction();
        switch (event.getAction()){
        case MotionEvent.ACTION_DOWN:
            pressed = true;
        break;

        case MotionEvent.ACTION_MOVE:
            //User is moving around on the screen
        break;

        case MotionEvent.ACTION_UP:
            pressed = false;
        break;
        }
        return pressed;
    }

Upvotes: 8

Related Questions