I would like to have 2 languages for the UI and separate string values for them in my resource file res\values\strings.xml
:
<string name="tab_Books_en">Books</string>
<string name="tab_Quotes_en">Quotes</string>
<string name="tab_Questions_en">Questions</string>
<string name="tab_Notes_en">Notes</string>
<string name="tab_Bookmarks_en">Bookmarks</string>
<string name="tab_Books_ru">Книги</string>
<string name="tab_Quotes_ru">Цитаты</string>
<string name="tab_Questions_ru">Вопросы</string>
<string name="tab_Notes_ru">Заметки</string>
<string name="tab_Bookmarks_ru">Закладки</string>
Now I need to retrieve these values dynamically in my app:
spec.setContent(R.id.tabPage1);
String pack = getPackageName();
String id = "tab_Books_" + Central.lang;
int i = Central.Res.getIdentifier(id, "string", pack);
String str = Central.Res.getString(i);
My problem is that i = 0
.
Why does not it work in my case?
Easier way is to use the
getString()
function within the activity.Reference: http://developer.android.com/guide/topics/resources/string-resource.html
I think this feature is added in a recent Android version, anyone who knows the history can comment on this.
Best Approach
App.getRes().getString(R.string.some_id)
Will work Everywhere (Utils, Models also).
I have read all the answers, all answers can make your work done.
getString(R.string.some_string_id)
in bothActivity
orFragment
.Context.getString(R.string.some_string_id)
where you don't have direct access togetString()
method. LikeDialog
.Problem
When you don't have
Context
access, like a method in yourUtil
class.Assume below method without Context.
Now you will pass
Context
as a parameter in this method and usegetString().
What i do is
What? It is very simple to use anywhere in your app!
So here is a Bonus unique solution by which you can access resources from anywhere like
Util class
.Add name field to your
manifest.xml
<application
tag.Now you are good to go. Use
App.getAppResources().getString(R.string.some_id)
anywhere in app.String myString = getString(R.string.mystring);
easy way
I would add something to the solution of leonvian, so if by any chance the string is not found among the resources (return value 0, that is not a valid resource code), the function might return something :
I have the same problem. But this code below works for me: Verify if your packageName is correct. You have to refer for the root package of your Android application.
The link you are referring to seems to work with strings generated at runtime. The strings from strings.xml are not created at runtime. You can get them via
getResources()
is a method of theContext
class. If you are inside aActivity
or aService
(which extend Context) you can use it like in this snippet.Also note that the whole language dependency can be taken care of by the android framework. Simply create different folders for each language. If english is your default language, just put the english strings into
res/values/strings.xml
. Then create a new foldervalues-ru
and put the russian strings with identical names intores/values-ru/strings.xml
. From this point on android selects the correct one depending on the device locale for you, either when you callgetString()
or when referencing strings in XML via@string/mystring
. The ones fromres/values/strings.xml
are the fallback ones, if you don't have a folder covering the users locale, this one will be used as default values.See Localization and Providing Resources for more information.