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?

- 1,091
- 3
- 16
- 34
-
Please refer this [1]: http://stackoverflow.com/questions/2680607/text-with-gradient-in-android i think this will helps you. – android_dev Jun 06 '13 at 09:22
-
@pankaj No, it is not a duplicate. I need to do it using an XML gradient not Java code gradient! – vergil corleone Jun 06 '13 at 09:24
3 Answers
This links solves your query:
It used LinearGradient class to create a shader ,which is set on the text view

- 1
- 1

- 4,509
- 9
- 42
- 66
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.
Shader textShader=new LinearGradient(0, 0, 0, 20,
new int[]{bottom,top},
new float[]{0, 1}, TileMode.CLAMP);//Assumes bottom and top are colors defined above
textPaint.setTextSize(textSize);
textPaint.setShader(textShader);
BoringLayout.Metrics boringMetrics=BoringLayout.isBoring(text, textPaint);
boringLayout=new BoringLayout(text, textPaint, 0, Layout.Alignment.ALIGN_CENTER,
0.0f, 0.0f, boringMetrics, false);
We then override onMeasure and onDraw:
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
setMeasuredDimension((int) textPaint.measureText(text), (int) textPaint.getFontSpacing());
}
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
boringLayout.draw(canvas);
}
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.

- 3,647
- 1
- 24
- 44
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:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<gradient
android:startColor="@color/gradient_start"
android:endColor="@color/gradient_end"
android:angle="-270" />
</shape>
The you would add it to the layout for your list item (the layout.xml file you define for this) like this code snippet:
<TextView
android:id="@+id/ranking_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/list_grad"
/>
...

- 3,647
- 1
- 24
- 44
-
1Only the text must have the gradient, not the whole textview... – vergil corleone Jun 06 '13 at 09:23
-
1