Reputation: 1759
I tried using getString()
to get a string from my string.xml
However. I just found the getText()
method can fetch HTML tags from my resources!
Say:
<string name="mySTring"><b><i>Hello Guys</i></b></string>
it surprised me, because I had to use Html.fromHtml()
to fetch the HTML tags - which is deprecated.
Which is the difference between the two methods?
Is there any advantage or disadvantage?
Upvotes: 40
Views: 12955
Reputation: 875
Edit*
You can use either getString(int)
or getText(int)
to retrieve a string. getText(int)
retains any rich text styling applied to the string
Some Differences..... according to the doc
getString(int id, Object... formatArgs)
Return the string value associated with a particular resource ID, substituting the format arguments as defined in Formatter and format(String, Object...). It will be stripped of any styled text information.
getString(int id)
Return the string value associated with a particular resource ID. It will be stripped of any styled text information.
getText(int id, CharSequence def)
Return the string value associated with a particular resource ID.
getText(int id)
Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.
Upvotes: 0
Reputation: 13865
From the doc,
For Resources.getString()
:
Return the string value associated with a particular resource ID. It will be stripped of any styled text information.
For Resources.getText()
:
Return the string value associated with a particular resource ID. The returned object will be a String if this is a plain string; it will be some other type of CharSequence if it is styled.
[Note that Context.getText()
and Context.getString()
internally calls the methods from Resources
.]
The doc says that getText()
retains the styling while the getString()
not. But you can use either one to get the string resource with HTML tags from strings.xml
, but the way is different.
Using Resources.getText():
strings.xml
:
<string name="styled_text">Hello, <b>World</b>!</string>
You can just call getText()
(note that it returns a CharSequence
not a String
, so it has the styling properties) and set the text to TextView
. No need for Html.fromHtml()
.
mTextView.setText(getText(R.string.styled_text));
But the doc says only a limited HTML tags, such as <b>, <i>, <u>
are supported by this method. The source code seems to suggest it supports more than that: <b>, <i>, <u>, <big>, <small>, <sup>, <sub>, <strike>, <li>, <marquee>, <a>, <font> and <annotation>
Using Resources.getString():
strings.xml
:
<string name="styled_text"><![CDATA[Hello, <b>World</b>!]></string>
You have to surround your string in a CDATA
block and calling getString
will return the string with HTML tags. Here you have to use Html.fromHtml()
.
mTextView.setText(Html.fromHtml( getString(R.string.styled_text)));
Html.fromHtml()
is deprecated in favor of a new method with flags
parameter. So use it like this:
HtmlCompat.fromHtml(getString(R.string.styled_text))
Implementation of the util method HtmlCompat.fromHtml
:
public class HtmlCompat {
public static CharSequence fromHtml(String source) {
if(Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
//noinspection deprecation
return Html.fromHtml(source);
} else {
return Html.fromHtml(source, Html.FROM_HTML_MODE_COMPACT);
}
}
}
Upvotes: 44
Reputation: 2564
According to the docs there is practically no difference other than
You can use either getString(int) or getText(int) to retrieve a string.
getText(int) will retain any rich text styling applied to the string.
So you would use getText for any localized resources for dealing with different language conversions for example but if you are just dealing with a string it literally doesn't matter.
Upvotes: 1
Reputation: 369
The way getString() implemented is to turn the data as string format ex "100" which take 100 as a string not an integer, The html tag is ignored as the method itself strip any tags. For getText method will capture all the TextView data or input from EditText. It's all about how you want to achieve yours program input and output handling with the right method.
Upvotes: 0