Pressed out button

Asked by At

I have class "myButton" that inherits from class "Button". Inside implemented method "onTouch", which works if you press the button. And I need to perform actions when you click wherever outside the button.

Is there any way to verify that the touch was made outside the button?

I came up with the idea to add a touch check to the View (full screen). But in this case, if you click on the button, two events "onTouch" will be activated: inside my class "myButton" and clicking on View.

1 Answers

0
Brent On

If you want to handle the push down and release on a button you can just do

    Button button = (Button) findViewById(R.id.button);
    button.setOnTouchListener(new View.OnTouchListener() {        
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch(event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                // PRESSED
                break; // if you want to handle the touch event
            case MotionEvent.ACTION_UP:
            case MotionEvent.ACTION_CANCEL
                // RELEASED
                break; // if you want to handle the touch event
        }
        return false;
    }
});

this will handle if a button is pushed down and if a button is released and if you just want pushed you can change accordingly

You have kotlin and java both tagged this is java so if you want it in kotlin just let me know

ps im returning false because if you dont you will circumvent the button's regular touch processing. Which means you will loose the visual effects of pressing the button down and the touch ripple. Also, Button#isPressed() will return false while the button is actually pressed.