We will use SpannableString to add different colors to a string and will use the setSpan(Object what, int start, int end, int flags) method of SpannableString to change the color for defined range of characters in that string.
Here is the code:
// this is the text we'll be operating on
TextView textView = new TextView(this);
SpannableString text = new SpannableString("String in Android");
// make String blue
text.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.blue_color)), 0, 5, 0);
textView.setText(text, BufferType.SPANNABLE);
TextView will display the text "String in Android" where "String" will be in blue color.
Hope this will help you :)
0 Comment(s)