Set color of TextView span in Android

203,009

Solution 1

Another answer would be very similar, but wouldn't need to set the text of the TextView twice

TextView TV = (TextView)findViewById(R.id.mytextview01);

Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");        

wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

TV.setText(wordtoSpan);

Solution 2

Here is a little help function. Great for when you have multiple languages!

private void setColor(TextView view, String fulltext, String subtext, int color) {
    view.setText(fulltext, TextView.BufferType.SPANNABLE);
    Spannable str = (Spannable) view.getText();
    int i = fulltext.indexOf(subtext);
    str.setSpan(new ForegroundColorSpan(color), i, i + subtext.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
}

Solution 3

I always find visual examples helpful when trying to understand a new concept.

Background Color

enter image description here

SpannableString spannableString = new SpannableString("Hello World!");
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(backgroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

Foreground Color

enter image description here

SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
spannableString.setSpan(foregroundSpan, 0, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

Combination

enter image description here

SpannableString spannableString = new SpannableString("Hello World!");
ForegroundColorSpan foregroundSpan = new ForegroundColorSpan(Color.RED);
BackgroundColorSpan backgroundSpan = new BackgroundColorSpan(Color.YELLOW);
spannableString.setSpan(foregroundSpan, 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
spannableString.setSpan(backgroundSpan, 3, spannableString.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
textView.setText(spannableString);

Further Study

Solution 4

If you want more control, you might want to check the TextPaint class. Here is how to use it:

final ClickableSpan clickableSpan = new ClickableSpan() {
    @Override
    public void onClick(final View textView) {
        //Your onClick code here
    }

    @Override
    public void updateDrawState(final TextPaint textPaint) {
        textPaint.setColor(yourContext.getResources().getColor(R.color.orange));
        textPaint.setUnderlineText(true);
    }
};

Solution 5

Set your TextView´s text spannable and define a ForegroundColorSpan for your text.

TextView textView = (TextView)findViewById(R.id.mytextview01);    
Spannable wordtoSpan = new SpannableString("I know just how to whisper, And I know just how to cry,I know just where to find the answers");          
wordtoSpan.setSpan(new ForegroundColorSpan(Color.BLUE), 15, 30, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);    
textView.setText(wordtoSpan);
Share:
203,009

Related videos on Youtube

hpique
Author by

hpique

iOS, Android & Mac developer. Founder of Robot Media. @hpique

Updated on July 08, 2022

Comments

  • hpique
    hpique almost 2 years

    Is it possible to set the color of just span of text in a TextView?

    I would like to do something similar to the Twitter app, in which a part of the text is blue. See image below:

    alt text
    (source: twimg.com)

  • hpique
    hpique almost 14 years
    Thanks! Is it possible to do this without assigning the text to the TextView first?
  • hpique
    hpique almost 14 years
    I didn't explain myself well. Let me rephrase. Are the first 3 lines necessary? Can't you create the Spannable object from the string directly?
  • Jorgesys
    Jorgesys almost 14 years
    Nop, you have to store your TextView's text into a Buffer Spannable to change the foreground colour.
  • Lukap
    Lukap almost 13 years
    I want to to the same thing with the color plus I want everything to be bold except the colored part, that part I want to be Italic , how can I do that ?
  • Piotr
    Piotr over 11 years
    Could you clarify how to use SpannableFactory? How "text" should look like?
  • Rishabh Srivastava
    Rishabh Srivastava almost 9 years
    how to set click in the span?
  • mostafa hashim
    mostafa hashim over 8 years
    but how can i change the color for multiple words i all text not one span?
  • Ashraf Alshahawy
    Ashraf Alshahawy over 8 years
    @mostafahashim create multiple spans by repeating line 3 wordtoSpan.setSpan(new ForegroundColorSpan(Color.RED), 50, 80, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
  • MBH
    MBH over 7 years
    after creating the Spannable by the SpannableFactory, so how to use it?
  • Eka putra
    Eka putra over 6 years
    getColor(int id) is deprecated on Android 6.0 Marshmallow (API 23) stackoverflow.com/questions/31590714/…
  • Muhaiminur Rahman
    Muhaiminur Rahman almost 5 years
    Nice Answer With Click Listener.
  • Dmitrii Leonov
    Dmitrii Leonov over 4 years
    Kotlin + Spannable String solution would look like this stackoverflow.com/questions/4032676/…
  • Abu Nayem
    Abu Nayem about 4 years
    you don't like Hasina 🤣
  • Ahsan Syed
    Ahsan Syed about 3 years
    Thankyou so much buddy you make my day :*
  • Suragch
    Suragch about 3 years
    @GkMohammadEmon, That sounds like custom painting and not a simple background color. Check out Bezier curves.
  • Gk Mohammad Emon
    Gk Mohammad Emon about 3 years
    Have you any good references or resources to about it? I need to add a curvey background of my notification action button. Thanks for your response
  • Suragch
    Suragch about 3 years
    @GkMohammadEmon, sorry I don't. It's been a while since I've done custom painting in Android since I moved on to Flutter.
  • Oleksandr Bodashko
    Oleksandr Bodashko almost 3 years
    Should be the best answer for Kotlin implementation.
  • Mohd Sakib Syed
    Mohd Sakib Syed over 2 years
    One of the best solution ever !!!
  • Vishal kumar singhvi
    Vishal kumar singhvi over 2 years
    Amazing implementation