1

I need to change drawable.xml colors programmatically. colorbutton.xml:

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android=" http://schemas.android.com/apk/res/android ">
<item android:state_pressed="true">
<shape>
<gradient
android:angle="90"
android:endColor="@color/ButtonBackground"
android:startColor="@color/ButtonBackground" />
<stroke
                android:width="3dp"
                android:color="#303030" />
            <corners
                android:radius="10dp" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>

    <item android:state_focused="true">
        <shape>
            <gradient
                android:angle="270"
                android:endColor="@color/ButtonBackground"
                android:startColor="@color/ButtonBackground" />
            <stroke
                android:width="3dp"
                android:color="#303030" />
            <corners
                android:radius="10dp" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>

    <item>
        <shape>
            <gradient
                android:angle="270"
                android:endColor="@color/ButtonBackground"
                android:startColor="@color/ButtonBackground" />
            <stroke
                android:width="3dp"
                android:color="#303030" />
            <corners
                android:radius="10dp" />
            <padding
                android:bottom="10dp"
                android:left="10dp"
                android:right="10dp"
                android:top="10dp" />
        </shape>
    </item>
</selector> 

I have tried:

Button moreTopics = (Button) myView.findViewById(R.id.appointmentMoreTopicsButton);

    StateListDrawable states = new StateListDrawable();

    int[] colors = {Color.parseColor("#FA0C28"), Color.parseColor("#FA0C28"), Color.parseColor("#FA0C28")};
    GradientDrawable drawable = new GradientDrawable();
    drawable.setColors(colors);

    states.addState(new int[] {android.R.attr.state_pressed}, drawable);
                        states.addState(new int[] {android.R.attr.state_focused}, drawable);
    states.addState(new int[] { }, drawable);

    moreTopics.setBackground(states);

but it seems that it overrides everything (stroke, corners and padding disappears) so maybe there is a way to achieve what I want exactly? I need to change only color but retain everything else. Maybe instead of StateListDrawable states = new StateListDrawable(); use StateListDrawable states = (StateListDrawable) moreTopics.getBackground();? but then I dont see methods which will help me to change the colors of states...

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Naujas Tewas
  • 73
  • 1
  • 9
  • Possible duplicate of [How can I change colors in my StateListDrawable?](https://stackoverflow.com/questions/19864337/how-can-i-change-colors-in-my-statelistdrawable) – Radost Nov 09 '18 at 12:14

0 Answers0