Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Text with gradient in Android

How would I extend TextView to allow the drawing of text with a gradient effect?

like image 464
Casebash Avatar asked Apr 21 '10 05:04

Casebash


People also ask

How do I color my text gradient?

Step 1: Apply a basic background to the body tag and align the text to center of the page. Step 2: Do some basic styling like font-size and family etc. Step 3: Apply the linear gradient property with any colors of your choice.

How do you color text on Android?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.


2 Answers

TextView secondTextView = new TextView(this); Shader textShader=new LinearGradient(0, 0, 0, 20,             new int[]{Color.GREEN,Color.BLUE},             new float[]{0, 1}, TileMode.CLAMP); secondTextView.getPaint().setShader(textShader); 
like image 61
Taras Avatar answered Oct 06 '22 00:10

Taras


I have used the top answer(@Taras) with a gradient of 5 colors, but there is a problem: the textView looks like that I have put a white cover on it. Here is my code and the screenshot.

        textView = (TextView) findViewById(R.id.main_tv);         textView.setText("Tianjin, China".toUpperCase());          TextPaint paint = textView.getPaint();         float width = paint.measureText("Tianjin, China");          Shader textShader = new LinearGradient(0, 0, width, textView.getTextSize(),                 new int[]{                         Color.parseColor("#F97C3C"),                         Color.parseColor("#FDB54E"),                         Color.parseColor("#64B678"),                         Color.parseColor("#478AEA"),                         Color.parseColor("#8446CC"),                 }, null, Shader.TileMode.CLAMP);         textView.getPaint().setShader(textShader); 

enter image description here

After many hours, I found out that I need to call textView.setTextColor() with the first color of the gradient. Then the screenshot:

enter image description here

Hope help someone!

like image 42
hanswim Avatar answered Oct 06 '22 00:10

hanswim