How to escape HTML attributes in strings.xml? - android

I'm supplying XML strings files to Android developers and confused about escaping rules when using HTML styles.
The documentation here only talks about embedding <b>, <i> and <u> tags without escaping, but it also shows that you can use Html.fromHtml with any string of HTML.
So how should a chunk of HTML like <font color="red">"Quote"</font> appear in the strings.xml file?
As font tags can have attributes and quotes should be escaped, how should attribute quoting be handled?
The following is invalid XML, so clearly this is wrong:
<string name="eg_1"><font color=\"red\">\"Quote\"</font></string>
Should just the XML attributes be left unescaped?
<string name="eg_2"><font color="red">\"Quote\"</font></string>
It's valid XML, but seems wrong to have different escaping rules in the string.
Perhaps CDATA should be used to simply protect the entire string?
<string name="eg_3"><![CDATA[<font color=\"red\">\"Quote\"</font>]]></string>
Or even just escape it?
<string name="eg_4"><font color=\"red\">\"Quote\"</font></string>

So how should a chunk of HTML like "Quote" appear in the strings.xml file?
Use CDATA, per your third sample above.

The first example is an invalid XML (as you said).
The second example is an invalid strings.xml file.
The third example should be like this:
<string name="eg_3"><![CDATA[<font color="red">"Quote"</font>]]></string>
The fourth example is fine.

Related

Why Colon before space is consider as Space but after Colon it is not Consider in Android

I have Simple Question regarding space define in string in string.xml file.
Let's say If we define string
<string name="regNumber">RegNumber : </string>
it will consider space before Colon not after. if we have put space after Colon then we have to define below way.
<string name="regNumber">RegNumber :\u0020</string>
or let's say this way also
<string name="regNumber">RegNumber : </string>
Question : why space is not Consider After Colon without writing any code ?
Brief Explanation :
If write this
<string name="regNumber">RegNumber : </string>
it will put space before Colon but not After the Colon
so the text will be RegNumber :
But My Question it will output like this it will not consider before Colon why it is consider.
RegNumber:
Because by default, android trim all the String to remove unnecessary spaces.. Say you accidentally pressed space bar and there were 20 spaces in your string file now,
<string name="regNumber">RegNumber : </string>
Android just trim the leading and trailing spaces. I never find any reason behind it. But I believe, it's useful to validate the string using Regex. [PS: I might be wrong]
See also: Android's documentation on formatting string
The regex defined is constructed to ignore white spaces at the beginning, and at the end. It is defined in the first phase of compiler building process called Lexical analysis.

how to superscript in a title in androidplot?

I'm trying to superscript a number in a title in androidplot, like so:
strings file:
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
xml file:
androidPlot.title="#string/plot_title_3m"
but it doesn't superscript at all, the number is normal styling. I've also tried using
<string name="plot_title_3m">stuff per m<sup>3</sup></string>
but no dice, it actually shows the sup tags in the title
also tried this
<string name="plot_title_3m">stuff per m<small><sup>3</sup></small></string>
and actually found out that the 'small' tags don't work either...
You can use HTML tags to achieve this. To be more exact, you need to use Spans on your input text to achieve this look on TextView, but we have a friendly neighborhood helper class that converts some common HTML tags into spans.
E.g.:
((TextView)findViewById(R.id.text)).setText(Html.fromHtml("X<sup>2</sup>"));

What's "msgid" and "xliff" in strings.xml file?

Background
Sometimes I see some weird attributes on the "strings.xml" file made by Google's samples, for example, on the chips example (code available here), I can find this strings file of "res/values-en-rGB" (for English-Britain) :
<resources xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:xliff="urn:oasis:names:tc:xliff:document:1.2">
<string name="more_string" msgid="8495478259330621990">"+<xliff:g id="COUNT">%1$s</xliff:g>"</string>
<string name="copy_email" msgid="7869435992461603532">"Copy email address"</string>
<string name="copy_number" msgid="530057841276106843">"Copy phone number"</string>
<string name="done" msgid="2356320650733788862">"Return"</string>
</resources>
I think both are used only for localized strings, as I never saw them inside "res/values" folder.
The question
What do those attributes mean?
What does the value of "xliff" mean?
When should you use them and what should you put there?
Are they even needed?
Is there any documentation about those things?
On Android Developers Localise your app page, search for xliff in the section named "Mark message parts that should not be translated".
The explanation is as follows:
Often strings contain contain text that should not be translated into
other languages. Common examples might be a piece of code, a
placeholder for a value, a special symbol, or a name. As you prepare
your strings for translation, look for and mark text that should
remain as-is, without translation, so that the translator doesn't
change it.
To mark text that should not be translated, use an
placeholder tag.
The suggestion is that text within the <xliff:g></xliff:g> tags should not be translated. These tags can also provide metadata about the non-translated text.
When you declare a placeholder tag, always add an id attribute that
explains what the placeholder is for. If your apps later replace the
placeholder value, be sure to provide an example attribute to clarify
the expected use.
For more information on the actual xliff tool, rather than how it relates to Android strings, check out the related question:
What does this mean "xmlns:xliff"? XML.

Formatting string in xml resource file

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?
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>
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!

How to make XML strings bold, underlined etc?

http://docs.fusioncharts.com/charts/contents/Styles/Font.html
I tried this, along with a lot of things but failed to do so.
Here's what I want.
<string name="ss">Bold. Underlined. Italic. Big. Small</string>
I want to format a little bit of the string.
Where it's written bold, I want it to be bold...and same for others.
I tried a lot of tags ...but well nothing worked, and I couldn't find anything on Google or SO.
I know how to do it in a textview, but that's not what I want...
I'm sending some text resource to an activity that shows it...
If I did it with different text views, I'd have to create several of them, a new one for whenever I want bold text, and that's not very elegant.
Is there a way to simple do this in the XML file ? or some other way ?
Try wrapping your marked up text in CDATA tags. For example:
<string name="ss"><![CDATA[<b>Bold.</b> <u>Underlined.</u> <i>Italic.</i> <big>Big.</big> <small>Small</small>]]></string>
And then use Html.fromHtml wherever you're wanting to display it:
Html.fromHtml(getString(R.string.ss))
This problem has been driving me crazy for ages. It's something sooo simple that you just want it to work!!!
Anyway I've found an answer here at http://www.coderzheaven.com/2011/06/19/styling-text-in-android-through-xml/
The key is to load the resource as a CharSequence using getResources().getText(R.string.xxxx) this will retain all the style information and allow you to use inline styling tags.
My mistake was using getString() because when loading your resource getString() will cause the string to lose all its style information.
exemple:
<string name="ss"><font size="15"><b>Parrainage</b></font><u>subscribe</u></string>
b = bold et u = underline .....etc
This is working for me.
<string name="welcome_messages">Hello, %1$s! You have <b>%2$d new messages</b>.</string>
txt.setText(Html.fromHtml(getString(R.string.welcome_messages)));
more details check Official site:
https://developer.android.com/guide/topics/resources/string-resource.html#StylingWithSpannables
in dimens file write:
<dimen name="size_edittext">180dp</dimen>
and in your xml layout or activity call it:
android:#dimen/ size_edittext

Categories

Resources