Handle button press state

275 Views Asked by At

I have a button in android application that has an accompanying red-circle TextView that represents a counter (iOS-style):

enter image description here

Button is a StateListDrawable so that when pressed is (visually) goes slightly down. I want accompanying red circle to go down as well.

I would be happy if I could found a callback that tells me when the button goes to pressed or unpressed (normal) state. I tried btn.setOnTouchListener, but it does not work. For instance, when I press button, it fires MotionEvent.ACTION_DOWN and that's good. But if I drag my finger out of button, it jumps back to normal state, but MotionEvent.ACTION_UP is not fired until I release my finger.

1

There are 1 best solutions below

0
On

You have two ways to do this

  1. Include Button and TextView in one layout and set click listener to that layout.

  2. Perform TextView click inside Button click listener. see below code.

    button.setOnClickListener(new OnClickListener() {
    
     @Override
      public void onClick(View v) {
            // TODO Auto-generated method stub
            textView.performClick();    
       }
    });