(Sorry for my language, I'm french)
I need to change the stroke color
of a Shape.
I've the same problem described here.
I need to change solid color when the EditText is not correct.
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="5dp"
android:bottomLeftRadius="5dp"
android:topLeftRadius="5dp"
android:topRightRadius="5dp"/>
<stroke android:width="2px" android:color="#CCCCCC"/>
</shape>
The answer :
GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setColor(Color.BLACK);
Problem in the answer is, I don't understand the rectangle item. If I replace it by the EditText, it is applied to the background, not the solid background.
Thanks in advance.
EDIT : My Bad, i want to change the Stroke color, not solid.
To set the shape stroke color programmatically this example uses GradientDrawable method setStroke(int width, int color) which sets the stroke width and change shape's color. Here default orange color of the shape drawable is changes programetically usingb GradientDrawable method setStroke(int width, int color).
android.graphics.drawable.GradientDrawable. A Drawable with a color gradient for buttons, backgrounds, etc. It can be defined in an XML file with the <shape> element. For more information, see the guide to Drawable Resources.
GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setStroke(2, Color.RED);
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