Development issue/problem:

How can I extend TextView to enable the drawing of text with a gradient effect?

How can I solve this problem?

Solution 1:

TextView secondTextView = new TextView(this) ;
textShader=new LinearGradient(0, 0, 20,
new int[]{Color.GREEN,Color.BLUE},
new float[]{0, 1}, TileMode.CLAMP) ;
secondTextView.getPaint().setShader(textShader) ;

Solution 2:

I used the top answer (@Taras) with a gradient of 5, but there is a problem: the textView looks like a white blanket I put over it. Here’s my code and a screenshot.

textView = (TextView) findViewById( ;
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[]{
}, null, Shader.TileMode.CLAMP);
textView.getPaint().setShader(textShader) ;

Text with gradient in Android –

After a few hours I realized that I had to call textView.setTextColor() with the first color of the gradient. Then a screenshot:

Text with gradient in Android –

I hope that helps somebody!

Solution 3:

It is not possible to expand the TextView to draw text with a gradient. However, this effect can also be achieved by making a cloth and drawing on it. First of all, we have to give up our personal unemployment insurance. To start with, we need to create a layout subclass. In this case we use BoringLayout, which only supports one line of text.

Shader textShader=new Linear gradient(0, 0, 0, 20,
new int[]{bottom, top},
new float[]{0, 1}, TileMode.CLAMP);// bottom and top colors are defined at the top
BoringLayout.metrics boringMetrics=BoringLayout.isBoring(text, textPaint);
boringLayout=new BoringLayout(text, textPaint, 0, Layout.Alignment.ALIGN_CENTER,
0.0f, 0.0f, boringMetrics, false) ;

Then we cancel OnMeasure and OnDraw:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec){
setMeasuredDimension((int) textPaint.measureText(text), (int) textPaint.getFontSpacing());

@Overdressed blank onDraw(Canvas){super.onDraw(Canvas);boringLayout.draw(canvas);}

Our onDraw implementation is pretty lazy at the moment (it completely ignores the measurement data!), but as long as you make sure the view has enough space, it should work fine.

You can also inherit the cloth and overwrite the onPaint method. Unfortunately, if you do this, the anchor for the rendered text will always be at the bottom, so we need to add -textPaint.getFontMetricsInt().ascent() to our y-coordinate.

Solution 4:

Here it is with a multi-line support in one line. This should also work for the buttons.

Shader = new Linear Gradient(0,0,0,textView.getLineHeight(),
startColor, endColor, Shader.TileMode.REPEAT) ;
textView.getPaint().setShader(shader) ;

Solution No 5:

I’ve compiled a library that contains both methods. You can create a GradientTextView in XML or simply use GradientTextView.setGradient(TextView textView…) to do this on a normal TextView object.

Solution No 6:

A simple but somewhat limited solution would be to use these attributes:

android: fadingEdge=horizontal
android: scrollHorizontally=true

I use it for text fields where I want them to hide if they get too long.

Solution No 7:

Here’s a good way to do it:

/*** setVerticalGradientOnTextView(final TextView tv, final int positionsAndColorsResId,final boolean viewAlreadyHasSize* only set true if textView already has a size*/public static void setVerticalGradientOnTextView(final TextView tv, final int positionsAndColorsResId,final boolean viewAlreadyHasSize) {final String[] positionsAndColors = tv.getContext().getResources().getStringArray(positionsAndColorsResId);final int[] colors = new int [positionsAndColors.length];float[] positions = new float [positionsAndColors.length];for (int i = 0; i** @param colors* the colors to be used. at least one of them must be present.* @param tv* textView to set the gradient to* @param position* where each color must be placed (fraction, max is 1). if it is zero, the colors are equally distributed.* @param viewAlreadyHasSize* is only enabled if textView all size*/public static empty setVerticalGradientOnTextView(final TextView tv, final int[] colors, final boolean viewAlreadyHasSize) {final Runningable = new Runnable() {final Runnable = new Runnable()

public void run() {
final TileMode tileMode_mode = TileMode.CLAMP;
final int height = tv.getHeight();
final LinearGradient lin_grad = new LinearGradient(0, 0, 0, height, colors, positions, tile_mode);
final Shader shader_gradient = lin_grad;
as (viewAlreadyHasSize);
runJustBeingDrawn(tv, runnable);

public void runJustBeforeBeingDrawn(last View, last Runnable runnable) { last OnPreDrawListener preDrawListener = new OnPreDrawListener() {@Overridepublic boolean onPreDraw() {view.getViewTreeObserver().removeOnPreDrawListener(this);;return true;}}};view.getViewTreeObserver().addOnPreDrawListener(preDrawListener);}

If you want to use a bitmap gradient card or a real gradient card, use it:

/**public empty setBitmapOnTextView(final TextView tv, final Bitmap bitmap) {final TileMode tile_mode = TileMode.CLAMP;final int height = tv.getHeight();final int width = tv.getWidth();final Bitmap-temp = Bitmap.createScaledBitmap(bitmap, width, height, true);final BitmapShader-bitmapShader = new BitmapShader(temp, tile_mode, tile_mode);tv.getPaint().setShader(bitmapShader);}.

EDIT: Alternative to running just before recording:

Solution No 8:

This is my private road. Version with text extension. screenshot

The LinearGradientForegroundSpan class extends the implementation of CharacterStyle UpdateAppearance {
private int startColor ;
private int endColor ;
private int lineHeight ;

public LinearGradientForegroundSpan(int startColor, int endColor, int lineHeight) {
this.startColor = startColor;
this.endColor = endColor;
this.lineHeight = lineHeight;

@DrawState(TextPaint tp) {tp.setShader(new LinearGradient(0, 0, 0, lineHeight,startColor, endColor, Shader.TileMode.REPEAT));} } } }.

The style of your story text.

SpanableString gradientText = new SpanableString(Gradient Text);
gradientText.setSpan(new LinearGradientForegroundSpan(Color.RED, Color.LTGRAY, textView.getLineHeight()),
0, gradientText.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
SpanableStringBuilder sb = new SpanableStringBuilder();
sb.append( normal text);

Solution No 9:

Here is an example of cutting lines, you can also use this example to display text and the source code will have no gradient, you get the source code and the added code from this one page –

Solution No 10:

I’ve found a way to do this without expanding the TextView classroom.

Main activity class : AppCompatActivity() {
private val textGradientOnGlobalLayoutListener = object : ViewTreeObserver.OnGlobalLayoutListener {Override fun in lobalLayout() {textGradient.paint.shader = LinearGradient.width.toFloat(),textGradient.height.toFloat(),color0, color1, Shader.TileMode.CLAMP)textGradient.viewTreeObserver.removeOnGlobalLayoutListener(this)}}textGradient by lazy {findViewById(}private trap color0 by lazy {ContextCompat.getColor(applicationContext, R.color.purple_200)}private trap color1 by lazy {ContextCompat.getColor(applicationContext, R.color.teal_200)}.

override fun onCreate(saveInstanceState: Bundle?) {

Good luck!

gradient textview android github,edit text gradient android,android linear gradient programmatically,text shadow in android,android textview monospace,lineargradient android,android textview shader,textalignment android,android textview design example,color with gradient android,animated rainbow textview github,gradient button android github,gradient animation android github,android textview gradient background,android gradient selector,android gradient shadow,gradient from top to bottom in android

You May Also Like

Back Up or Restore Data On Your Samsung Galaxy Note 10/10+

Learn how to easily back up and restore your data for Samsung…

How to fix “We Couldn’t Create the Outlook Data File” Issue?

Office Outlook is an email client that allows you to manage users’…

Considerations About The Best Integrated Amplifier Under 2000 Dollars – Review & Buying Guide 2021

For hi-fi lovers, music is their life and the only thing that…

Can You Play Xbox Games On PC?

Not too long ago, Microsoft gave fans of Xbox games the ability…