Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why getAction method always return ACTION_DOWN?

I am using onTouchListener for a layout. I want to take the click outside the layout. I set onTouchListetener for the layout. But motion event always shows ACTION_DOWN. Even i touchOutside the view, It is not showing ACTION_OUTSIDE. Could anyone help me to find out why it is not showing constant ACTION_OUTSIDE. Here is the code i am using

Layout.setOnTouchListener(new View.OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Log.i("action",event.getAction()+"");
        if (event.getAction() == MotionEvent.ACTION_OUTSIDE) {
            Toast.makeText(getApplicationContext(), "check", Toast.LENGTH_SHORT).show();
            return true;
        }

        return false;
    }
});
like image 694
Sanal V Avatar asked Feb 19 '26 03:02

Sanal V


2 Answers

If you return false, then you're signifying that you do not wish to receive further touch events. You need to return true to continue getting motion events.

like image 162
Jason Robinson Avatar answered Feb 21 '26 15:02

Jason Robinson


Very simple to fix by adding this:

 override fun onTouchEvent(event: MotionEvent): Boolean {
        return true
    }
like image 20
Serg Burlaka Avatar answered Feb 21 '26 17:02

Serg Burlaka



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!