I need to change the stroke color from the app. The user is able to change the background color so I need to also let them change the stroke (outline) of the button. As its is already set in the drawable (sample below) I have not found a way to change this. Seems like all of the other questions like this just said to use the XML file.... but that doesnt let me make it dynamic. Thank you for any help!
I need to change the stroke color to a user defined color. Nothing to do with the state.
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffffff"/>
<stroke
android:width="3dp"
android:color="@color/Dim_Gray" /> <<<<--- This is what I need to change
<padding android:left="10dp"
android:top="10dp"
android:right="10dp"
android:bottom="10dp"
/>
<corners android:bottomRightRadius="12dp" android:bottomLeftRadius="12dp"
android:topLeftRadius="12dp" android:topRightRadius="12dp"/>
</shape>
Try using StateLists (as opposed to ColorStateList). Take a look: http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
You can also create a
ShapeDrawable
(or aRoundRectShape
in your example) programmatically, and then call the button'ssetBackgroundDrawable
I answered a similar question in Change shape border color at runtime
Its like the same solution proposed by f20k but in my case the drawable was a GradientDrawable instead of a ShapeDrawable.
see if it works...
1. If you have drawable file for a "view" like this
Then you can change
a. Stroke color :
b. Solid color :
2. If you have drawable file for a "view" like this
Then you can change the individual item attributes by taking separate drawable objects by there positions.
now to change stroke or solid color :
Please look at the LayerDrawable because it created from your XML and used at runtime.
Here is a Demo Example:
You can modify it at runtime like:
Perhaps they are referring to Color State Lists which allows you to change the color based on whether the button was pressed/focused/enabled/etc