Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 993
user3252731 Avatar asked Jan 30 '14 10:01

user3252731


People also ask

How do I change the stroke color on my Android?

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

What is gradient drawable?

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.


1 Answers

GradientDrawable myGrad = (GradientDrawable)rectangle.getBackground();
myGrad.setStroke(2, Color.RED);
like image 127
vipul mittal Avatar answered Oct 12 '22 01:10

vipul mittal