0

I have a String array list set up to populate text to multiple intro screens. As the text is populated in the actualy Java class and not in the xml resource file I want to know how can I make certain words in the text bold from within the java class?

 public String [] slide_descriptions = {
        "I want this text in bold \n",
        "and maybe this too!"
};
2
  • 1
    check this stackoverflow.com/questions/7130619/… Commented May 8, 2019 at 20:54
  • Thats for xml, I need to make it bold directly into the java file Commented May 9, 2019 at 8:43

3 Answers 3

1

You can use Html tags. For example:

textView.setText(Html.fromHtml("< b >This is Bold< /b >"));

For String, do it like this:

 public String [] slide_descriptions = {
        "<b>I want this text in bold</b> \n",
        "<b>and maybe this too!</b>"
};

So at the end when you put the string in the textview:

textView.setText(Html.fromHtml(slide_description));
Sign up to request clarification or add additional context in comments.

3 Comments

Lux you use html with TextView not String. I edited the answer
You can't just add <b> into the string otherwise it just prints <b> out with the rest of the string.......
Lux you are textView.setText(Html.fromHtml(slide_description));
0

See answer below: link

However in comment says that This will slow down performance.

3 Comments

Thats all for the xml file, I cant write like that in the java file?
What do you mean "in java file"? You need bold text inside java file? Not on the output?
There's to files the java class and the xml. I'm using the java to populate a slider screen. So there's one xml file but 5 slider screens. So all the text that is displayed is stored in the java class as a string and populated into a textview box. So I need to highlight specific words in the java string in the java class. I don't know how to do that.
0

You can use spannable string as it is easy and less costly

SpannableString spannableString = new SpannableString(yourstringhere);
spannableString.setSpan(new StyleSpan(Typeface.BOLD),indexStart,indexEnd, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

2 Comments

Can you add that spannable string code into the code I put in my question so I can see how it's structured / implemented
String stringToBold = "I want this text in bold"; SpannableString spannableString = new SpannableString(stringToBold); spannableString.setSpan(new StyleSpan(Typeface.BOLD),0,stringToBold.length, Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.