Reputation: 2424
I have a number of background drawables that the only different is the color of the border or the color of the background.
Is there a way to define a general drawable and then add (or change) the attribute I need.
For example here is a rectangular drawable
XML file
<?xml version="1.0" encoding="utf-8"?>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="@dimen/radius_small"/>
<solid android:color="@color/simple_black" />
<stroke android:width="2dip" android:color="@color/simple_white" />
<padding
android:left="@dimen/two_dp"
android:right="@dimen/two_dp"/>
</shape>
and I would like to change the solid color or the stroke color, and not to create a separate drawable for each one.
Upvotes: 3
Views: 1734
Reputation: 3086
This assumes that you want to do it programatically. If all you need is to change the background and/or stroke color, here is how you can do it:
Obtain a reference to the shape
GradientDrawable drawable = (GradientDrawable)context.getResources().getDrawable(R.drawable.shape_id)
To change color
drawable.setColor(Color.RED)
To change stroke color and width (notice that you can only change both of them together, so I recommend keeping a variable with the stroke's width in px)
drawable.setStroke(Util.dpToPx(context, 2), Color.YELLOW)
Upvotes: 2