set Selector for Button Programmatically issues

Vamsi Challa picture Vamsi Challa · Feb 8, 2015 · Viewed 12.5k times · Source

I have a row of buttons and i am setting their selectors for background and text programatically. The reason i want to do this programmatically is because, I have a set of themes the user can choose from and depending upon the theme selected, i want to change the selector for the button.

For example, if the user selects a blue theme, when loaded, the background of the button is blue and text colour is white. When he presses the button, the background changes to white and the text colour changes to blue. When user removes the finger from button, the changes revert back to default blue for background and white for text colour. You can see the respective selectors for blue below.

This is similar to all other themes. I have separate XMLs for all the themes. The selector for text colour change works fine. The problem is with the background selector for button.

selector_background_blue.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@android:color/white" android:state_pressed="true"/>
    <item android:drawable="@color/blue_500"/>

</selector>

color_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:state_pressed="true" android:color="@color/blue_500"/>
    <item android:color="@android:color/white"/>

</selector>

I have a class that returns the drawable(selector) depending upon the theme selected. I am getting the selector as follows:

public Drawable getButtonBackgrounds(String theme) {
    Drawable drawable = null;

    if (theme.equalsIgnoreCase(Const.Theme.BLUE))
        drawable = context.getResources().getDrawable(
                R.drawable.selector_background_blue);

    return drawable;
}

I'm setting these selector for button's background as follows:

private void setButtonBackgrounds(Drawable buttonDrawable) {
int sdk = android.os.Build.VERSION.SDK_INT;

        if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
            btnA.setBackgroundDrawable(buttonDrawable);
            btnT.setBackgroundDrawable(buttonDrawable);
            .....
            .....
            btnVoice.setBackgroundDrawable(buttonDrawable);
        } else {
            btnA.setBackground(buttonDrawable);
            btnT.setBackground(buttonDrawable);
            .....
            .....
            btnVoice.setBackground(buttonDrawable);
        }
}

button's xml:

<Button
    android:id="@+id/btnT"
    android:layout_width="0dip"
    android:layout_height="match_parent"
    android:layout_weight="0.20"
    android:background="?android:attr/selectableItemBackground"
    android:text="@string/button_t"
    android:textSize="22sp" />

Total Row's XML:

<LinearLayout
            android:layout_width="match_parent"
            android:layout_height="0dip"
            android:layout_weight="1" >

            <Button
                android:id="@+id/btnA"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:text="@string/arithmetic_symbol"
                android:textSize="16sp" />

            <Button
                android:id="@+id/btnT"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:text="@string/trigonometric_symbol"
                android:textSize="16sp" />

            <Button
                android:id="@+id/btnN"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:text="@string/voice_calculator_symbol"
                android:textSize="16sp"
                android:visibility="gone" />

            <ImageButton
                android:id="@+id/btnVC"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:contentDescription="@string/empty"
                android:src="@drawable/ic_keyboard_voice_black"
                android:text="" />

            <Button
                android:id="@+id/btnC"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:text="@string/button_c"
                android:textSize="16sp" />

            <Button
                android:id="@+id/btnD"
                android:layout_width="0dip"
                android:layout_height="match_parent"
                android:layout_weight="0.20"
                android:background="?android:attr/selectableItemBackground"
                android:text="@string/button_del"
                android:textSize="16sp" />
        </LinearLayout>

This is the same for all the buttons in the row.

The drawable is set just fine on the load. Please refer to the image below.

enter image description here

The problem is When I click on a button(for ex., A), the adjacent ImageButton(microphone) is also changing its state. Please look at the images below:

enter image description here enter image description here enter image description here enter image description here

Why is this happening? Can someone help me with this. Please let me know if you need any other info.

Answer

bonnyz picture bonnyz · Feb 10, 2015

I think that you are experiencing a mutate-related issue (please take a look here, it's extremly useful)

You need to call mutate() on your drawable before assingning it to the View if yout don't want to share the common state across the various instances:

Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.btn);
buttonDrawable.mutate()
btnA.setBackgroundDrawable(buttonDrawable);

In your code you are using the same Drawable for more then one View, so you need to adopt the approach I've described above to avoid the state-sharing.