Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: User Interface: LinearGradient not working with custom colors

I am trying to add linear gradient to the text in my TextView using Shader and LinearGradient. This is working only if I use standard colors like Color.LTGRAY, Color.MAGENTA, etc. No gradient is shown if I use colors from my custom_colors.xml. Any ideas how can I make this work for custom colors?

Shader txtShad=new LinearGradient(0, 0, 0, 10, new int[]{R.color.el_amethyst,R.color.el_maroon}, null, TileMode.MIRROR);
textview_dummy.getPaint().setShader(txtShad);

Below is the custom_colors.xml:

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <color name="el_maroon">#CD2990</color>
  <color name="el_amethyst">#9D6B84</color>
</resources>
like image 281
Ankit Avatar asked Mar 13 '13 13:03

Ankit


People also ask

How do I set gradient color in TextView?

To set the color of the TextView, you can also use Gradient color by using the LinearGradient. The parameters of the LinearGradient are: x0: x-coordinate for start of gradient line. It is of type float.


1 Answers

Try this:

Shader txtShad = new LinearGradient(0, 0, 0, 10, new int[] { getResources().getColor(R.color.el_amethyst), getResources().getColor(R.color.el_maroon) }, null, TileMode.MIRROR);

TL;DR you need to resolve the color resources first

like image 182
Ahmad Avatar answered Sep 27 '22 18:09

Ahmad