Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

759
Views
How to set gradient as text color as well as add stroke around it in TextView Android?

I want to set gradient as textcolor and also at the same time I want text to have a solid stroke around it as well in a TextView. So far what I have achieved is text can only show gradient or the stroke, not both at the same time.

I've created a custom class with extended TextView and I am using the following methods:

Using this to draw stroke:

paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(_strokeWidth);
setTextColor(_strokeColor);

Which gives me this result:

enter image description here

Using following code to add gradient:

Shader textShader = new LinearGradient(0f, 0f, getWidth(), getTextSize(), gradientColorsArray, null, Shader.TileMode.CLAMP);
                paint.setShader(textShader);

And It gives me the following result:

enter image description here

The problem is when I combine the above two methods, The stroke is drawn but the color of the stroke is the same as the gradient I am giving to paint object.

Following is the result I want to achieve. It would be great if somebody can guide me on how to achieve desired results.

enter image description here

over 4 years ago · Santiago Trujillo
4 answers
Answer question

0

So after waiting for more than 4 days and lots of research, I am finally able to succeed to achieve the desired output.

The mistake I was making is while drawing a stroke on a paint object, I am setting the stroke color as textcolor. What I did this time was I created a LinearGradient() object and gave it to paint.shader while setting the paintStyle(Paint.Style.Stroke).

Paint paint = this.getPaint();
        paint.setStyle(Paint.Style.STROKE);
        paint.setStrokeWidth(5f);
        paint.setShader(new LinearGradient(0f, 0f, getTextSize(), getTextSize(), mOutlineColor, mOutlineColor, Shader.TileMode.CLAMP));

And after setting the stroke in onDraw() method of my CustomTextView class, I called the super.onDraw(canvas)

Then I create a new LinearGradient() object for the gradient colors as follow:

 Paint paint = this.getPaint();
        paint.setStyle(Paint.Style.FILL);
        Shader linearShader = new LinearGradient(0f, 0f, getWidth(), getTextSize(), colors, null,
                Shader.TileMode.CLAMP);
        paint.setShader(linearShader);

Finally calling the super.onDraw(canvas) again and this gives my textview a stroke as well as gradient as textColor.

over 4 years ago · Santiago Trujillo Report

0

Hola Internet para todos

about 4 years ago · Manuel saravia Report

0

Hola a tos amigos

about 4 years ago · Manuel saravia Report

0

Saludos para todos los del grupo

about 4 years ago · Manuel saravia Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!