Asked  7 Months ago    Answers:  5   Viewed   47 times

I would like to have 2 languages for the UI and separate string values for them in my resource file resvaluesstrings.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?

 Answers

90

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

String mystring = getResources().getString(R.string.mystring);

getResources() is a method of the Context class. If you are inside a Activity or a Service (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 folder values-ru and put the russian strings with identical names into res/values-ru/strings.xml. From this point on android selects the correct one depending on the device locale for you, either when you call getString() or when referencing strings in XML via @string/mystring. The ones from res/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.

Tuesday, June 1, 2021
 
keisar
answered 7 Months ago
90

For resource ids, you can use:

Glide.with(fragment)
    .load(R.drawable.resource_id)
    .into(imageView);

For assets, you can construct an asset uri:

Glide.with(fragment)
    .load(Uri.parse("file:///android_asset/<assetName>"))
    .into(imageView);
Sunday, August 1, 2021
 
MM1
answered 4 Months ago
MM1
13

Use the following functions to add shared preferences and to fetch the saved values from all activities.

public static void setDefaults(String key, String value, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    SharedPreferences.Editor editor = preferences.edit();
    editor.putString(key, value);
    editor.commit();
}

public static String getDefaults(String key, Context context) {
    SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
    return preferences.getString(key, null);
}
Sunday, August 8, 2021
 
Adam Rackis
answered 4 Months ago
56

You can update the label on TextView or anything similar.

  • Get the updated string using update_str = Context.getResources().getString (R.string.update_identifier)
  • Update the label using ((TextView)findViewById (R.id.textview)).setText (updated_str)
Friday, August 13, 2021
 
madphp
answered 4 Months ago
12

It appears there isn't a more straightforward way to accomplish this.

Saturday, September 18, 2021
 
vlatkozelka
answered 3 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share