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!"
};
Tamir Abutbul
  • 7,301
  • 7
  • 25
  • 53
Lux
  • 25
  • 4
  • 1
    check this https://stackoverflow.com/questions/7130619/bold-words-in-a-string-of-strings-xml-in-android – Bosco May 08 '19 at 20:54
  • Thats for xml, I need to make it bold directly into the java file – Lux May 09 '19 at 08:43

3 Answers3

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));
Yunus Kulyyev
  • 1,022
  • 15
  • 26
0

See answer below: link

However in comment says that This will slow down performance.

Michael
  • 113
  • 3
  • 13
  • Thats all for the xml file, I cant write like that in the java file? – Lux May 09 '19 at 08:43
  • What do you mean "in java file"? You need bold text inside java file? Not on the output? – Michael May 09 '19 at 10:16
  • 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. – Lux May 09 '19 at 11:51
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);
Vikas Sharma
  • 685
  • 8
  • 18
  • Can you add that spannable string code into the code I put in my question so I can see how it's structured / implemented – Lux May 10 '19 at 18:18
  • `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);` – Vikas Sharma May 10 '19 at 18:19