Simulate an Android button at the software level

I saw this route

View.performClick(); 

but it does not show the actual button click. I also tried this method,

 btn.setPressed(true); btn.invalidate(); 

but it just shows the pressed button. I narrowed it down to this code, which pushes and releases, but does not click. Am I missing something? How can I make a full click, as if the user was clicking (monkeyrunner is not an option now)

 btn = (Button) findViewById(R.id.btn_box); Handler handler = new Handler(); Runnable r = new Runnable() { public void run() { btn.setPressed(true); btn.invalidate(); Handler handler1 = new Handler(); Runnable r1 = new Runnable() { public void run() { btn.setPressed(false); btn.invalidate(); } }; handler1.postDelayed(r1, 1000); } }; handler.postDelayed(r, 1000); 
+4
source share
1 answer

Your code is ok. just add btn.performClick(); after invalidate();

And for a better view, you can reduce the time of handler1 .

+2
source

All Articles