How would I extend TextView
to allow the drawing of text with a gradient effect?
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.
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.
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);
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);
After many hours, I found out that I need to call textView.setTextColor()
with the first color of the gradient. Then the screenshot:
Hope help someone!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With