Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the difference between getString() and getText()?

Tags:

html

android

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?

like image 216
Shahin Ghasemi Avatar asked Aug 12 '17 07:08

Shahin Ghasemi


People also ask

What does Getstring do exactly?

Return a localized string from the application's package's default string table.

What is the return type of GetText () method?

GetText returns the text from the single-line text field. It returns only the first line of a multi-line text field. If you include iStartChar but not iNumChars , GetText returns the text from the iStartChar character to the end of the text.

How do I GetText in Java?

The GetText class provides a standard mechanism for extracting text from WLS log message catalogs. Messages are identified by their message id, and may be qualified by a subsystem name. The basic format of the String objects returned by the GetText methods is the message body of the requested message.


2 Answers

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);         }     }  } 
like image 110
Bob Avatar answered Sep 28 '22 06:09

Bob


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.

like image 40
user685590 Avatar answered Sep 28 '22 05:09

user685590