I want to access color resource defined as drawable resource and desire to toggle the background color in JAVA, basically background of a button was changed using below mentioned drawable XML. I tried accessing button and modify color attribut but this changed the shape of button to normal square shape. I want to keep shape as defined in drawable XML and change background color manually.
<?xml version="1.0" encoding="UTF-8"?>
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="#EAEAEA" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
<item><shape android:shape="rectangle">
<solid android:color="#EAEA00" />
<corners android:bottomLeftRadius="8dip"
android:bottomRightRadius="1dip"
android:topLeftRadius="1dip"
android:topRightRadius="8dip" />
</shape>
</item>
You have 2 possibilities:
myButton.setBackgroundColor(Color.CHOOSE_ONE);
myButton.setBackgroundResource(R.color.youCustomColor);
If you want to set the color from an hexadecimal value just use the static method of the Color
class:
myButton.setBackgroundColor(Color.parseColor("#RRGGBB"));
//http://developer.android.com/reference/android/graphics/Color.html#parseColor%28java.lang.String%29
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With