0

My problem is similar to question posted here. I want an Android button to stay pressed. I cannot follow the solution provided because onClick will be called when a button is pressed via keyboard or trackball and i need to handle that.

I tried setting button.setPressed(true); in onClick callback , but it doesn't seem to work. Is there a way to do this?

Community
  • 1
  • 1
Yagna
  • 423
  • 1
  • 5
  • 15

2 Answers2

2

Try this it will work...

singIn.setOnTouchListener(new OnTouchListener() {
                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if(event.getAction()==MotionEvent.ACTION_DOWN) return true;
                    if(event.getAction()!=MotionEvent.ACTION_UP) return false;

                  //DO SOMETHING!!

                    singIn.setPressed(true);                    
                    return true;
                }


            });
Aditya Nikhade
  • 1,373
  • 10
  • 15
1

Try sending a touch event to the button like this:

MotionEvent down = MotionEvent.obtain(SystemClock.uptimeMillis(), SystemClock.uptimeMillis(), MotionEvent.ACTION_DOWN, 0, 0, 0);
yourButton.dispatchTouchEvent(down);
newbyca
  • 1,523
  • 2
  • 13
  • 25