2

I have to put in the same textView a string with substrings of different sizes and colors. How can I do?

enter image description here

Ortensia C.
  • 4,666
  • 11
  • 43
  • 70

4 Answers4

2

You need to create a String with HTML tags and set this String to TextView as Html.fromHtml like below:

((TextView) findViewById(R.id.txt)).setText(
                        Html.fromHtml("<Font color=\'" + "#457548"
                                + "'\" >" + "your text"
                                + "</Font>"), TextView.BufferType.SPANNABLE)

More information go to: how-display-html-android

M D
  • 47,665
  • 9
  • 93
  • 114
0

You could attach html notation to the text, or put a textview for each different size and color in a linearlayout, why does it have to be strictly in one textview?

Angmar
  • 599
  • 4
  • 9
0

If you don't want to use HTML, you can use spannables in a custom view.

Have a look at some old questions those can give you some hints:

  1. spannable on android for textView
  2. Android EditText : How to apply Foreground color span as you type?
Community
  • 1
  • 1
Simone Casagranda
  • 1,217
  • 17
  • 26
0

Android has SpannableString that can be set in a textView. Each SpannableString can have a different color, font , onCLick() and so on. Multiple SpanableStrings can be set to a single texview.

Aditya Kushwaha
  • 839
  • 6
  • 12