0

I have a gradient color xml (green_gradient.xml) defined in Res\Color folder. How can I get it to paint to a bar at custom View. For normal green color, I paint like

paint.setColor(Color.GREEN);

But how to replace Color.GREEN with green_gradient.xml defined in Res\Color folder.

EDIT 1. My green_gradient.xml is in res\color folder.

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_pressed="true" >
        <shape>
            <solid
                android:color="#70c656" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="3dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
    <item>
        <shape>
            <gradient
                android:startColor="#70c656"
                android:endColor="#53933f"
                android:angle="270" />
            <stroke
                android:width="1dp"
                android:color="#53933f" />
            <corners
                android:radius="4dp" />
            <padding
                android:left="10dp"
                android:top="10dp"
                android:right="10dp"
                android:bottom="10dp" />
        </shape>
    </item>
</selector>
batuman
  • 7,066
  • 26
  • 107
  • 229

2 Answers2

0

Assuming you have defined GREEN color in your custom color set, you can do

<your_context>.getResources().getColor(R.color.GREEN);

And if the color is altogether in a different xml having different color states then you can use something like

XmlResourceParser parser =context.getResources().getXml(R.color.green_gradient);
ColorStateList colors = null;
try {
    colors = ColorStateList.createFromXml(context.getResources(), parser);
} catch (XmlPullParserException e) {            
    e.printStackTrace();        
} catch (IOException e) {
    e.printStackTrace();
}

mybutton.setTextColor(colors); 
Rajeev
  • 1,374
  • 1
  • 11
  • 18
  • but since you are setting the color to paint, I donot see any method there which takes ColorStateList as input. – Rajeev Jul 14 '13 at 06:17
  • I cant't set ColorStateList colors; to paint.setColor(colors); I have compile error. – batuman Jul 14 '13 at 06:32
  • So you mean, drawing in custom View can't use the color designed in xml. – batuman Jul 14 '13 at 06:47
  • my intention is to get gradient color. I think I have to design gradient color again for custom View in the program. – batuman Jul 14 '13 at 06:51
  • No, at a bar. android:background is I just present as an example of easiness in xml. I draw the bar on the View and want to paint gradient color. – batuman Jul 14 '13 at 07:01
  • @batuman check this if you want to set the gradient color to paint http://stackoverflow.com/questions/8600805/how-to-set-gradient-style-to-paint-object. – Raghunandan Jul 14 '13 at 07:02
0

Make your green_gradient.xml in drawable folder and then you can access it programmatically using R.drawable.green_gradient. in a layout xml file you can access it as @drawable/green_gradient

Bishan
  • 15,211
  • 52
  • 164
  • 258