This question already has an answer here:
- Text with gradient in Android 8 answers
I have a file containing a gradient( textgradient.xml
) in my drawable folder. I need to put this gradient as the text color of a TextView
through Java. How to do that?
You just need to create a drawable resource (see an example below), and add it to the layout you created for your ListItem.
The drawable (in your res\drawable folder - name it whatever - listgrad.xml for ex) could look like:
The you would add it to the layout for your list item (the layout.xml file you define for this) like this code snippet:
...
It doesn't appear possible to extend TextView to draw text with a gradient. It is, however, possible to achieve this effect by creating a canvas and drawing on it. First we need to declare our custom UI element. In the initiation we need to create a subclass of Layout. In this case, we will use BoringLayout which only supports text with a single line.
We then override onMeasure and onDraw:
Our implementation of onDraw is at this point quite lazy (it completely ignores the measurement specs!, but so long as you guarantee that the view is given sufficent space, it should work okay.
Alternatively, it would be possible to inherit from a Canvas and override the onPaint method. If this is done, then unfortunately the anchor for text being drawn will always be on the bottom so we have to add -textPaint.getFontMetricsInt().ascent() to our y coordinate.
This links solves your query:
Text with gradient in Android
It used LinearGradient class to create a shader ,which is set on the text view