1

i search from while for giving gradient to textView in android.but not getting proper solution any where.

i have the screen like thisthe text in the image is actually having the gradient

if i put the images as text, but i have many textview which having gradient,then application may be large in size. so if anybody know the solution please post it. Thanks.

Rahul Shirphule
  • 989
  • 3
  • 14
  • 36

1 Answers1

9

Here It goes,

int[] color = {Color.DKGRAY,Color.CYAN};
        float[] position = {0, 1};
        TileMode tile_mode = TileMode.REPEAT;
        LinearGradient lin_grad = new LinearGradient(0, 0, 0, 35,color,position, tile_mode);
        Shader shader_gradient = lin_grad;
        your_text_view.getPaint().setShader(shader_gradient);

Edited:

Hi @android developer,

If you want to do some vertical or any gradients to the view,

Please use,

RadialGradient instead of LinearGradient.

Only thing is you have to adjust,

x coordinate of the center of the radius.
y coordinate of the center of the radius.

and also,

radius value of the view.

and use Tile Mode as Mirror instead of Repeat.

I have sample but it may not exact of what you want,

TileMode tile_mode = TileMode.MIRROR or TileMode.REPEAT;
        RadialGradient rad_grad = new RadialGradient(0, 3, 5, Color.BLUE,Color.RED,tile_mode, tile_mode);
        Shader shader_gradient = rad_grad;
        your_text_view.getPaint().setShader(shader_gradient);

And you can use SweepGradient also instead of LinearGradient.

Kartihkraj Duraisamy
  • 3,171
  • 2
  • 25
  • 36