Professional Documents
Culture Documents
Stack Overflow
log in
65
android-textview
android-textattributes
share
Asked
Jan 17 '13 at 2:02
Krushna Chulet
797 4 8
Edited
Feb 26 '14 at 11:37
Possible duplicate of Is it possible to have multiple styles inside a TextView? Ciro Santilli Feb 8 at 13:29
add a comment
3 Answers
135
Order By
Votes
share
Answered
Jan 17 '13 at 2:04
it works, finally I can move forward. thanks man. Budi Darmawan Jan 17 '13 at 2:20
Graet! It's so a beautiful trick... +1 Ali Bagheri Shakib May 16 '14 at 19:21
why this doesn't work mBox..setText(Html.fromHtml("<b>" + name + "</b>") + doNotApplyHTML); any idea thanks Shan Xeeshi Aug 19 '14 at 10:05
78
While you can use Html.fromHtml() you can use a more native approach which is SpannableStringBuilder , this post may be helful.
final SpannableStringBuilder str = new SpannableStringBuilder("Your awesome text");
str.setSpan(new android.text.style.StyleSpan(android.graphics.Typeface.BOLD), INT_START, INT_END, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
TextView tv=new TextView(context);
tv.setText(str);
share
wtsang02
9,255 4 32 56
Answered
Jan 17 '13 at 2:09
Edited
Oct 28 '13 at 14:36
Whoever downvoted has no idea how Android works. ;) Martn Marconcini Aug 12 '14 at 22:32
Very nice. Always much better to go with a native pure Android solution. The above HTML version may work, but it's not nearly as elegant. idratherbeintheair Sep 5 '14 at 23:59
If this answer doesn't works try to append your text with spannable string some thing like : mSpannable.append(yourremainingtext); Gowtham Kumar Mar 9 '15 at 8:11
add a comment
12
I thought that the chosen answer didn't provide a satisfactory result. I have written my own function which takes 2 strings; The full text and the
part of the text you want to make bold.
It returns a SpannableStringBuilder with the 'textToBold' from 'text' bolded.
I find the ability to make a substring bold without wrapping it in tags useful.
/**
* Makes a substring of a string bold.
* @param text
Full text
* @param textToBold Text you want to make bold
* @return
String with bold substring
*/
public static SpannableStringBuilder makeSectionOfTextBold(String text, String textToBold){
SpannableStringBuilder builder=new SpannableStringBuilder();
if(textToBold.length() > 0 && !textToBold.trim().equals("")){
//for counting start/end indexes
String testText = text.toLowerCase(Locale.US);
String testTextToBold = textToBold.toLowerCase(Locale.US);
int startingIndex = testText.indexOf(testTextToBold);
int endingIndex = startingIndex + testTextToBold.length();
//for counting start/end indexes
if(startingIndex < 0 || endingIndex <0){
share
Answered
Jul 15 '14 at 9:08
Derek Beattie
7,378 2 19 39
Edited
Jun 28 '15 at 16:53
Can you provide an example of how to use this in code? MicroR Oct 22 '15 at 16:19
@MicroR You need to pass two Strings, the second String needs to be a section of text contained within the first. e.g. makeSectionOfTextBold("This is some great text.", "great").
Leon Oct 30 '15 at 16:09
add a comment
Your Answer
log in
or
Name
By posting your answer, you agree to the privacy policy and terms of service.
meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc