user3252731
user3252731

Reputation: 123

How to change stroke color dynamically?

(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.

Upvotes: 12

Views: 16224

Answers (1)

vipul mittal
vipul mittal

Reputation: 17401

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setStroke(2, Color.RED);

Upvotes: 25

Related Questions