1

I want to display help dialog box in which it has one textview and it loads the content from the String.xml file. Instead of making it one boring paragraphs, I would like to add some formatting to that String.xml For example coloring some sentences, bold..etc. Is there a way I can do that in the xml file within the string?

My xml looks like that

 <string name="help_summary">Clicking on button (Summary) will result in ((report))</string>

So I want (Summary) to be red color and ((report)) to be bold.

How can I achieve that?

Snake
  • 14,228
  • 27
  • 117
  • 250

2 Answers2

0

You can use Html. When you load the string in the textview use:

yourTextView.setText(Html.fromHtml(context.getResources().getString(R.string.help_summary)));

And use html in the string, for example:

<string name="help_summary"><![CDATA[Clicking on button <span style="color:red">Summary</span> will result in <b>report</b>]]></string>
xtheosirian
  • 190
  • 1
  • 11
  • Thank you! I don't have to use the [CDATA though right? – Snake Apr 01 '14 at 03:28
  • It's the best way to deal with html imo. See [this](http://stackoverflow.com/questions/3235131/set-textview-text-from-html-formatted-string-resource-in-xml/18199543#18199543) for more information. – xtheosirian Apr 01 '14 at 03:40
0

There are some attributes that can be used in string resources without implementing HTML into and or altering your java code. None needed, you CAN do most of what you seem to need in strings.xml Example:

<string name ="my_string"><i>italics</i><b>bold</b><u>underline</u><font fgcolor="#FFFFFFFF">color</font><small>small text</small></string>

I believe there is a <large/> or <big/> tag as well, amongst a few more.

Edit also, there is \n for a new line. Just include that in your string like so:

<string name="paragraph">Text is one first line \n now text is on next line. \n\n  this will appear as an indented paragraph now.</string>

Hope this helps, happy coding!

MattMatt
  • 905
  • 6
  • 19
  • Thank you MattMatt , but according to the following link the guy was not able to use fgcolor attribute like that without converting from html http://stackoverflow.com/questions/22118558/color-string-resource-for-xml-layout?rq=1 – Snake Apr 01 '14 at 04:07
  • I have used it directly in my apps on Google Play and know it works. This is typed from mobile, and off the top of my head, it may not be fgcolor. I will look at my code tomorrow and post it. Meanwhile, see for yourself. I didn't use any HTML in this app https://play.google.com/store/apps/details?id=high.caliber.productions.root – MattMatt Apr 01 '14 at 04:17
  • I apologize, it seems the `fgcolor` attribute is not supported in ~ 4.3+, maybe earlier. It shows up on some of my older devices, not my actual 4.4.2 device – MattMatt Apr 03 '14 at 01:55
  • Yes there is a bug raised in the android issue tracker about this. So i guess back back to html :( – Snake Apr 03 '14 at 04:58