12

Is it possible to have texts with different sizes, font-types or styles in the same TextView ?

Something like this:

| myLogin logout |

user2647856
  • 123
  • 1
  • 1
  • 4

3 Answers3

17

You can do this using:

textView.setText(Html.fromHtml("<b>myLogin</b> <i>logout</i>"));

For more options, look into SpannableString: Link

With SpannableString, you can apply multiple formatting to a single string.

This article will be very helpful to you: Rich-Style Formatting of an Android TextView

Hakan Yildizhan
  • 142
  • 1
  • 8
Vikram
  • 50,508
  • 11
  • 88
  • 119
17

For anyone who wants to do this without the HTML formatting , use a SpannableString.

As in :

SpannableString styledString = new SpannableString("myLogin logout");
styledString.setSpan(new StyleSpan(Typeface.BOLD), 0, 7, 0);
styledString.setSpan(new StyleSpan(Typeface.ITALIC), 8, 14, 0);

TextView tv = (TextView)findViewById(R.id.tv);
tv.setText(styledString);

More examples can be found here

cmak
  • 513
  • 1
  • 4
  • 13
Neeraj
  • 2,124
  • 1
  • 19
  • 35
-2

Unless you make a custom TextView, no. Consider using two different TextViews if you do not want to create a custom TextView.

Alex Fu
  • 5,431
  • 3
  • 30
  • 40