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...