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
  • 172
  • 1
  • 9
Vikram
  • 51,313
  • 11
  • 93
  • 122
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
  • 576
  • 1
  • 4
  • 15
Neeraj
  • 2,376
  • 2
  • 24
  • 41
-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,509
  • 3
  • 31
  • 40