Search code examples
androidandroid-layoutuser-interfacebuttonontouchlistener

How to handle an action when a user removes a finger from a button?


I need to handle the action when the user pulls a finger off the button by clicking on it, but does not release it. I tried to use OnTouchListener, but, as I understood it, it only processes the movement of a finger or the release of a button.


Solution

  • It's existing the OnTouchListener also:

    view.setOnTouchListener(new View.OnTouchListener() {
       @Override
       public boolean onTouch(View v, MotionEvent event) {
           if(event.getAction() == MotionEvent.ACTION_MOVE) {
               Rect r = new Rect(0, 0, view.getWidth(), view.getHeight());
               if (!r.contains((int)event.getX(), (int)event.getY()))
                   // your action
               return true;
           }
    
           return false;
       }
    });