Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TextView with different fonts and styles?

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

Something like this:

| myLogin logout |

like image 319
user2647856 Avatar asked Aug 14 '13 20:08

user2647856


2 Answers

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

like image 98
Vikram Avatar answered Oct 14 '22 00:10

Vikram


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

like image 38
Neeraj Avatar answered Oct 14 '22 00:10

Neeraj