Sie sind auf Seite 1von 5

sign up

Stack Overflow

log in

Questions Tags Users Badges Unanswered Ask

Read this post in our app!

65

how to make a specific text on TextView BOLD


android

android-textview

android-textattributes

I don't know how to make a specific text on TextView become BOLD.


its like this
txtResult.setText(id+" "+name);

I want the output to be like this:


1111 neil
id and name are variables that I have retrieved the value from database, and I want to make the id to bold, but only the id so the name will not affected, I

have no idea how to do this.

share

improve this question


Budi Darmawan
353 1 4 7

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

Just build your String in HTML and set it:


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

share

improve this answer


Raghav Sood
56.8k 15 120 138

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

Thanks man it works smoothely Arslan Ahmad Feb 11 '15 at 5:09

truly, an elegant trick. thanks! Mill Feb 27 '15 at 13:58


show 1 more comment

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

improve this answer

wtsang02
9,255 4 32 56

Answered
Jan 17 '13 at 2:09

Edited
Oct 28 '13 at 14:36

Please explain downvote. wtsang02 Apr 10 '14 at 22:19

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

improve this answer


Eitan
123 1 5

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

Email

By posting your answer, you agree to the privacy policy and terms of service.

Post Your Answer

meta chat tour help blog privacy policy legal contact us full site
Download the Stack Exchange Android app
2016 Stack Exchange, Inc