Button stays in pressed state after OnTouch callback

86 Views Asked by At

I'm defining a CompoundDrawable on a button and reacting for clicks on it:

    mFileSelector.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        final int DRAWABLE_LEFT = 0;
        final int DRAWABLE_TOP = 1;
        final int DRAWABLE_RIGHT = 2;
        final int DRAWABLE_BOTTOM = 3;

        if (event.getAction() == MotionEvent.ACTION_UP) {
            if (event.getRawX() >= (mFileSelector.getRight() - mFileSelector.getCompoundDrawables()[DRAWABLE_RIGHT].getBounds().width())) {
                mFileSelector.setSelected(false);
                mFileSelector.setPressed(false);
                mFileSelector.clearFocus();
                return true;
            }
        }
        return false;
    }

As you can see I've tried setSelected, setPressed and clearFocus but none of them did any good. The button stays in 'pressed' state after positive onTouch (meaning I consumed the touch event)

1

There are 1 best solutions below

3
Dinorah Tovar On

What you need is to change the background of your button, something like this will help you.

 if (enabled) {
        your_button?.background = ContextCompat.getDrawable(this, R.drawable.round_button_blue)
    } else {
        your_button?.background = ContextCompat.getDrawable(this, R.drawable.round_button_gray)
    }