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
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
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
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
- Explain the meaning of Span flags like SPAN_EXCLUSIVE_EXCLUSIVE
- Android Spanned, SpannedString, Spannable, SpannableString and CharSequence
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);
Related videos on Youtube
Author by
hpique
iOS, Android & Mac developer. Founder of Robot Media. @hpique
Updated on July 08, 2022Comments
-
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:
(source: twimg.com) -
hpique almost 14 yearsThanks! Is it possible to do this without assigning the text to the TextView first?
-
hpique almost 14 yearsI 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 almost 14 yearsNop, you have to store your TextView's text into a Buffer Spannable to change the foreground colour.
-
Lukap almost 13 yearsI 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 over 11 yearsCould you clarify how to use SpannableFactory? How "text" should look like?
-
Rishabh Srivastava almost 9 yearshow to set click in the span?
-
mostafa hashim over 8 yearsbut how can i change the color for multiple words i all text not one span?
-
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 over 7 yearsafter creating the Spannable by the SpannableFactory, so how to use it?
-
Eka putra over 6 yearsgetColor(int id) is deprecated on Android 6.0 Marshmallow (API 23) stackoverflow.com/questions/31590714/…
-
Muhaiminur Rahman almost 5 yearsNice Answer With Click Listener.
-
Dmitrii Leonov over 4 yearsKotlin + Spannable String solution would look like this stackoverflow.com/questions/4032676/…
-
Abu Nayem about 4 yearsyou don't like Hasina 🤣
-
Ahsan Syed about 3 yearsThankyou so much buddy you make my day :*
-
Suragch about 3 years@GkMohammadEmon, That sounds like custom painting and not a simple background color. Check out Bezier curves.
-
Gk Mohammad Emon about 3 yearsHave 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 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 almost 3 yearsShould be the best answer for Kotlin implementation.
-
Mohd Sakib Syed over 2 yearsOne of the best solution ever !!!
-
Vishal kumar singhvi over 2 yearsAmazing implementation