I have a String in my strings.xml file such as
<string name="my_text">Hello <b>$%1$s</b>, android is cool <b>bold me</b></string>
The only way to retrieve the text with styling for bolding is using
resources.getText(R.string.my_text);
Although the issue is getText does not take additional parameters for the arguments I wish to provide such as the method getString that takes arguments such as
resources.getString(R.string.my_text, "I WILL BE BOLDED")
If I use getString I lose the bold, if I use getText I cant pass arguments how do I obtain both?
To expand upon Sagar's answer, set up the string resource using CDATA
:
<string name="test"><![CDATA[When no one was looking, Lex Luthor took <b>%s</b> cakes. He took %d cakes. That\'s as many as %s %s. And that\'s terrible.]]></string>
Then, retrieve the string resource using getString()
and use HtmlCompat
to handle the formatting:
HtmlCompat.fromHtml(getString(R.string.test, "forty", 40, "four", "tens"), HtmlCompat.FROM_HTML_MODE_COMPACT)
NOTE: The author of the answer can neither confirm nor deny that some number of cakes were consumed in the creation of this answer. However, no actual supervillains were involved, as far as you are aware.
strings.xml
<string name="my_text">Hello <b>$%1$s </b>, android is cool <b>bold me </b></string>
code
String.format(res.getString(R.my_text.welcome_messages), "I WILL BE BOLDED");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With