Reputation: 423
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...
Upvotes: 15
Views: 13665
Reputation: 682
In kotlin:
binding.titleTextView.text = HtmlCompat.fromHtml("This is a " + "<b>" + "your_string" + "</b>",HtmlCompat.FROM_HTML_MODE_LEGACY)
Upvotes: 0
Reputation: 62549
i've had success using getText instead of getString. It seems to maintain styling.
You can use either getString(int) or getText(int) to retrieve a string. getText(int) retains any rich text styling applied to the string.
Upvotes: 11
Reputation: 423
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 "<" + ";" (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.
Upvotes: 25
Reputation: 392
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;
Upvotes: -1
Reputation: 5266
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>
Upvotes: 7
Reputation: 9754
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
Upvotes: 1
Reputation: 5865
Try this ..
String myString = "This is a " + "<b>" + "your_string" + "</b>";
textview.setText(Html.fromHtml(myString));
for more see here
Upvotes: 1