Joshua Abrams
Joshua Abrams

Reputation: 387

Detect when a user moves off of a button in Android

Is it possible to detect anytime a user moves their finger off of a button after they have been pressing down on it?

I have determined that onTouchListener will get an action when the user moves off of the button vertically (It gives an action: ACTION_CANCEL), but I have not been able to find a way to determine when a user moves off the button horizontally.

public boolean onTouch(View arg0, MotionEvent arg1) {
    switch(arg1.getAction()){
    case MotionEvent.ACTION_CANCEL:
    case MotionEvent.ACTION_UP:
        switch(arg0.getId()){
        case R.id.my_button:
            button.setBackgroundResource(R.drawable.untouched);
            break;
        }
        break;
    case MotionEvent.ACTION_DOWN:
        switch(arg0.getId()){
        case R.id.my_button:
            button.setBackgroundResource(R.drawable.touched);
            break;
        }
        break;
    }
    return false;
}

Upvotes: 2

Views: 969

Answers (1)

ihrupin
ihrupin

Reputation: 6972

Try go next:

arg1.getX();
arg1.getY();

arg1 - is from your code

Here you can get coordinates of MotionEvent. Next you will do anything with this coordinates

Hope, it help you

Upvotes: 1

Related Questions