14

In my string.xml file, I have something like this:

<string name="example_string"><b>This</b> is a <b>%1$s</b></string>

And then I placed it into a TextView:

textView.setText(getString(R.string.example_string, "good question"));

The "good question" argument that I passed to the getString() method is not shown in bold. Even the word "This" is not shown in bold! What's the reason for this and how to solve it?

=========================================================================

I know how to use Html.fromHtml(), but this method does not support inserting a string to the place holder that I have defined in the string resource. If you are trying to tell me that Html.fromHtml() exists, please DO NOT REPLY...

Thomas W Chen
  • 403
  • 1
  • 4
  • 12
  • 2
    I think getString() method somehow deletes or wipes out all the formatting information stored in string resouce when it returns the result... So I guess a better question to ask is: Is there a better method out there that is able to fill the place holder in string resource while keep the formatting information in there? – Thomas W Chen Jul 25 '16 at 14:24

7 Answers7

24

So after a day of search, I found the desired answer on Android Developers website! The Link to the page is here: https://developer.android.com/guide/topics/resources/string-resource.html

Sometimes you may want to create a styled text resource that is also used as a format string. Normally, this won't work because the String.format(String, Object...) method will strip all the style information from the string. The work-around to this is to write the HTML tags with escaped entities, which are then recovered with fromHtml(String), after the formatting takes place.

Basicaly, the change that I would make based on my original question is to replace the bracket "<" with the HTML tag for that, which is "&lt" + ";" (Type them together in your xml file! I had to separate them because StackOverflow will diplay the tag as the bracket itself.) For more detailed explanation, please see the Styling with HTML markup section from the website that I posted above.

Thomas W Chen
  • 403
  • 1
  • 4
  • 12
10

i've had success using getText instead of getString. It seems to maintain styling.

from the docs:

You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.

j2emanue
  • 60,549
  • 65
  • 286
  • 456
7

You need to format the text using Html.fromHtml or using a SpannableString.

Html.fromHtml example:

textView.setText(Html.fromHtml(getString(R.string.example_string, "good question")));

Edit:

If you're having trouble passing the tags through, its because it needs to be escaped by CDATA.

eg. <string name="example_string">This is a <![CDATA[<b>%1$s</b>]]></string>

biddulph.r
  • 5,226
  • 3
  • 32
  • 47
1

Try this ..

String myString = "This is a " + "<b>" + "your_string" + "</b>"; 
textview.setText(Html.fromHtml(myString));

for more see here

Community
  • 1
  • 1
Uttam Panchasara
  • 5,735
  • 5
  • 25
  • 41
  • Thanks for the reply. I know this technically SHOULD work, (and normally I would use this method), but in this case I must use the getString() method to fill in the place holder that's stored in the string resource. – Thomas W Chen Jul 25 '16 at 14:20
  • try to set directly in your xml – Uttam Panchasara Jul 25 '16 at 14:28
  • That's exactly what I did in the first place... The simply got wiped out after getString() method inserted my string to the place holder. – Thomas W Chen Jul 25 '16 at 14:30
0

Do like this below

String boldText = "Hello"+"<b>" + "StackOverflow" + "</b>";
textView.setText(Html.fromHtml(boldText));

Now your text look like this : Hello StackOverflow

See this link

Vinoth Vino
  • 9,166
  • 3
  • 66
  • 70
  • Hi I know how to use Html.fromHtml()... It's just I don't want to use hard-coded string, and instead put my string in the resource folder and then fill the place holder in the code (This is also a better programming practice :)) – Thomas W Chen Jul 25 '16 at 14:27
0

In kotlin:
binding.titleTextView.text = HtmlCompat.fromHtml("This is a " + "<b>" + "your_string" + "</b>",HtmlCompat.FROM_HTML_MODE_LEGACY)

-1
use this... it will working.

/**
 * 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){
            return builder.append(text);
        }
        else if(startingIndex >= 0 && endingIndex >=0){

            builder.append(text);
            builder.setSpan(new StyleSpan(Typeface.BOLD), startingIndex, endingIndex, 0);
        }
    }else{
        return builder.append(text);
    }

    return builder;
BarmanInfo
  • 392
  • 1
  • 18
  • Thanks dude, at least you are not thinking that I don't even know about Html... This method is cumbersome though. I have already defined what should be bold in my string resource, so I guess the best solution would be more towards showing the that I have defined on screen :) – Thomas W Chen Jul 25 '16 at 14:52