AlexIIP
AlexIIP

Reputation: 2481

Change button's background color via selector and manual performClick()

I have a button with the following background.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
  <item android:state_pressed="true"
   android:drawable="@drawable/easy_button_pressed" /> <!-- pressed -->
  <item android:state_focused="true"
   android:drawable="@drawable/easy_button_pressed" /> <!-- focused -->
  <item android:drawable="@drawable/easy_button_default" /> <!-- default -->
</selector>

This works great if I manually press the button. But I also at times need to manually call button.performClick(); When I do this, the button does not change the color. Can someone please help me out.

Upvotes: 0

Views: 1300

Answers (1)

kushyar
kushyar

Reputation: 1191

After calling performClick(), call view.setPressed(true); and then set it to false after a few miliseconds like below:

handler.postDelayed(new Runnable() {

    @Override
    public void run() {
        view.setPressed(false);
    }
}, 100);

Upvotes: 3

Related Questions