11

Why is the following code not working? It works in a Toast but not in a TextView. boldName doesn't show up as bold when I run my program but it does show up as bold when I set it to a Toast. Does anyone have any other solutions?

String boldName = "<b>" + name + "</b>";
Spanned conBold = Html.fromHtml(boldName);
chosen_contact.setText("You have chosen " + conBold + " as your contact.");
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
user3131263
  • 173
  • 1
  • 2
  • 8
  • You may find solution form your problem here. http://stackoverflow.com/questions/10828182/spannablestringbuilder-to-create-string-with-multiple-fonts-text-sizes-etc-examp – Keyfe Ang Dec 31 '13 at 02:45
  • You're sure that works in a Toast? It doesn't for me (assuming my edit was what your code actually is). – Michael Yaworski Dec 31 '13 at 02:59

2 Answers2

25

I'm honestly not sure why exactly TextViews act the way they do where you can set it all bold as you are doing, but only if they entire TextView is bold, yet you can't if only part of it is bold and there are other Strings in there.

However, this code will work for you:

// a SpannableStringBuilder containing text to display
SpannableStringBuilder sb = new SpannableStringBuilder("You have chosen " + name + " as your contact.");

// create a bold StyleSpan to be used on the SpannableStringBuilder
StyleSpan b = new StyleSpan(android.graphics.Typeface.BOLD); // Span to make text bold

// set only the name part of the SpannableStringBuilder to be bold --> 16, 16 + name.length()
sb.setSpan(b, 16, 16 + name.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); // make first 4 characters Bold

chosen_contact.setText(sb); // set the TextView to be the SpannableStringBuilder
Michael Yaworski
  • 13,410
  • 19
  • 69
  • 97
  • It works! Thank you so much! However, while you're code is very well documented, I'm having trouble understanding a few concepts. This statement confuses me the most: sb.setSpan(b, 16, 16 + name.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE); – user3131263 Dec 31 '13 at 03:49
  • `16` is the position of the String to start bolding. `16 + names.length()` is where to stop bolding. So I said start bolding after `"You have chosen "` and stop bolding the length of the name positions after where it started. `b` is the type of span to apply on the StringBuilder (which is bold). Visit http://developer.android.com/reference/android/text/Spannable.html and http://developer.android.com/reference/android/text/SpannableStringBuilder.html to see some more information. Please accept my answer if you are satisfied with it. – Michael Yaworski Dec 31 '13 at 03:53
  • 1
    Thank you for your thorough explanation. I have accepted your answer. Happy New Years! – user3131263 Dec 31 '13 at 20:00
0

You may Use SpannableStringBuilder because it implements from spannable and CharSequence, also you may do anything with following

TextView txtTest = (TextView) findViewById(R.id.txt);
String text = "This is an example";    
final SpannableStringBuilder str = new SpannableStringBuilder(text);
str.setSpan(new TypefaceSpan("monospace"), 0, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new TypefaceSpan("serif"), 9, 12, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.white)), 0, 5, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.grey)), 6, 8, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), 0, 3, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
txtTest.setText(str);

I have add colors.xml in values

<color name="black">#000000</color>
<color name="grey">#DCDCDC</color>
<color name="white">#FFFFFF</color>
Abdul Rahman Majeed
  • 1,125
  • 2
  • 10
  • 22