Reputation: 41
In my application I am using lot of buttons. And all buttons must be in same shape but different in color. I wrote drawable for each button with same features but different solid android:color. This makes more
than 20 drawable xml in my app.
How can I use the same drawable for all buttons, but should be able to
set different background color.
I tried as below, but did not work.
1. In attrs.xml
In drawable folder created buttonshape.xml for rectangle shape
In styles.xml
@drawable/buttonshape
@color/mycolor
And finally in my activity xml, for the button I set the theme as
<Button android:id="@+id/btnTest"
android:theme="@style/TestTheme" android:text="Test" />
This did not work for me. Could anyone of you please help me to sort this out? I am a new to android development. I am not able to figure out where I am going wrong. Any help would be really appreciated. Thanks, Roger
Upvotes: 4
Views: 1083
Reputation: 440
use this: GradientDrawable bgShape = (GradientDrawable)view.getBackground().getCurrent(); bgShape.setColor(Color.BLACK);
where ever you want to set different color of any view, just use this code.
.getCurrent gives the selected drawable layerlist
use this it will not throw java.lang.ClassCastException: android.graphics.drawable.GradientDrawable cannot be cast to android.graphics.drawable.ShapeDrawable exception.
it works perfectly for me.
Upvotes: 4