Reputation: 31090
In my Android app I simulate a button click with the following code :
void clickButton(final Button b, int delay)
{
final Handler handler = new Handler();
handler.postDelayed(new Runnable()
{
public void run()
{
final Drawable drawable = b.getBackground();
b.performClick();
b.getBackground().setColorFilter(b.getContext().getResources().getColor(R.color.colorAccent), PorterDuff.Mode.MULTIPLY);
// b.setBackgroundColor(Color.rgb(88, 166, 198));
b.setPressed(true);
b.invalidate();
// delay completion till animation completes
b.postDelayed(new Runnable() { //delay button
public void run() {
b.setPressed(false);
b.invalidate();
b.setBackground(drawable);
//any other associated action
}
}, 800); // .8secs delay time
}
}, delay);
}
But the color of the button will stay green after a click, how to get it back to the color before the click, after a .5 sec delay?
Upvotes: 0
Views: 31
Reputation: 4859
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.colorAccent));
b.postDelayed(new Runnable() {
public void run() {
b.setBackgroundColor(ContextCompat.getColor(b.getContext(), R.color.initialColor));
}
}, 800);
Upvotes: 1