Loading string resources in Android
Sunday, January 18th, 2009 - 1:50 pm - Android
To support i18n, internationalization, Android provides a resource file, usually /res/values/strings.xml. That XML and others are compiled and each resource is assigned a unique integer ID. The IDs are placed into a resource file, “R”, as public static final variables. Here’s how to get the value of a string resource in your application.
In a layout XML
android:text="@string/resource_name"
In an Activity
this.getString(R.string.resource_name)
In an area of the program in which you have access to a Context or Application, such as a ListAdapter.
context.getString(R.string.resource_name)
application.getString(R.string.resource_name)
This is great. Thank you so much. I had been trying to access a string resource from the java code for a couple of hours already and could not figure it out.
how can I use “@string” here ? I am a newbee..
for example, you have a like this…
So, u need to create a string variable in strings.xml that can be found at : projectname/res/layout/values/strings.xml
what to do? there are two ways of doing this :
1. Resources tab :
– Click ADD button
– Choose STRING
– Give the name same as you did in the layout : title
– Give the value of that string such as : this is the title
2. via code in strings.xml file tab
– Just add this syntax :
This is the title
** But you can just do like this in your layout :
Hope its could help you… 🙂
This concept is not intuitive. Your blog was a lifesaver!
TQ… i really appreciate this!!
Sir, I got “R.java was modified manually!Reverting to generated version!” message when I changed “String r = getResources().getString(R.String.nameHere); ” line and saved the project…
So whats wrong with it?
thank you…
[…] Here. LD_AddCustomAttr("AdOpt", "1"); LD_AddCustomAttr("Origin", "other"); LD_AddCustomAttr("theme_bg", "ffffff"); LD_AddCustomAttr("theme_text", "333333"); LD_AddCustomAttr("theme_link", "0066cc"); LD_AddCustomAttr("theme_border", "f9f9f9"); LD_AddCustomAttr("theme_url", "114477"); LD_AddCustomAttr("LangId", "1"); LD_AddCustomAttr("Autotag", "technology"); LD_AddCustomAttr("Tag", "android"); LD_AddSlot("wpcom_below_post"); LD_GetBids(); Like this:LikeBe the first to like this post. […]
ooo u r a saviour
In case there is not a Context or Activity handy:
final Resources r = Resources.getSystem();
r.getString(android.R.string.untitled);
can also do
String r = getResources().getString(R.String.nameHere);
to skip the 2nd line
this seems so extraneous. I thought doing something like:
EditText textBox = (EditText)findViewById(R.id.textBox);
and then
String strMessage = textBox.getText().toString();
would have done it… but so far only your way works, so, thanks!
MrSqueezles: You need to watch out for memory leaks on orientation change (or other environment changes) when you do that.
Googling “Android memory leak” gets a page by Romain Guy that explains it well.
Thanks for pointing that out, David, but I think I covered it. amidroid, please note that I mentioned holding on to the Application. Don’t hold an instance of an Activity. You can get the Application by calling
this.getApplication()
from an Activity.what for other classes? which do not have access to context or application. e.g. an independent class.
amidroid, if you don’t have access to a context, you can’t read data from it. You could hold an instance of the application in whatever class needs it or make some kind of a proxy.
Thanks!
Thanks, that’s what I searched for. Had a crazy hack around to include two string resources in one TextView, like load the first, save the text to a string, load the other and append the first… Haha this is really easy 🙂
I’m glad you found it helpful, Sonja. It took me a lot of searching through the API before I realized it was in front of my face 😛