0

is it possible to change font size and color of string in the same textview .

For example :

 _______________
|_______________| --> this is my text view 

What i want to do

________________
|ABC (red color) |
|abc(whitecolor) |
|________________|

And string should look like this "ABC\nabc"

podgradle
  • 336
  • 1
  • 10
  • 17
  • Possible duplicate of [How to set the text color of TextView in code?](https://stackoverflow.com/questions/4602902/how-to-set-the-text-color-of-textview-in-code) – Pavneet_Singh Mar 03 '18 at 15:16
  • 1
    Possible duplicate of [Set color of TextView span in Android](https://stackoverflow.com/questions/3282940/set-color-of-textview-span-in-android) – ADM Mar 03 '18 at 15:17
  • [Real dupe](https://stackoverflow.com/questions/6094315/single-textview-with-multiple-colored-text) – Pavneet_Singh Mar 03 '18 at 15:18

1 Answers1

1

Use SpannableStringBuilder

SpannableStringBuilder builder = new SpannableStringBuilder();

SpannableString str1= new SpannableString("Text1");
str1.setSpan(new ForegroundColorSpan(Color.RED), 0, str1.length(), 0);
builder.append(str1);

SpannableString str2= new SpannableString(appMode.toString());
str2.setSpan(new ForegroundColorSpan(Color.WHITE), 0, str2.length(), 0);
builder.append(str2);

TextView tv = (TextView) view.findViewById(android.R.id.text1);
tv.setText( builder, TextView.BufferType.SPANNABLE);
Rainmaker
  • 10,294
  • 9
  • 54
  • 89