0

I have the following TextView:

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="This must be bold (but not this)" />

I want only a part of the text to be bold. android:textStyle="bold" will therefore not work.

Vingtoft
  • 13,368
  • 23
  • 86
  • 135

2 Answers2

2

If the text is coming from a string resource, use <b> tags as you would in HTML:

<string name="foo"><b>This must be bold</b> (but not this)</string>

If the text is coming from somewhere else, use a SpannableStringBuilder to wrap the bold portion in a StyleSpan set for boldface. Or, generate an HTML edition of the string with <b> tags and use Html.fromHtml() to create the Spanned. In either case, the Spanned/SpannableStringBuilder output then just gets passed to setText(), which will rendered the desired portion in bold.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
1

You can do it but I think it is very handy with xml therefore you can create a string in html and do it like this:

String sourceString = "<b>" + id + "</b> " + name; mytextview.setText(Html.fromHtml(sourceString));

Where id and name will be two different text in your class

We're All Mad Here
  • 1,544
  • 3
  • 18
  • 46