Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access the values from strings.xml dynamically?

Tags:

java

android

What I want to do is to get a specific text from strings.xml dynamically. I think it will involve to access an object variable dynamically. There will be a function like:

public void getDynamicString(int level) {
  text.setText(R.string.levelText_+level);
}

And in strings.xml there will be <string name="levelText_5">Text level 5</string>

I would rather not create a list with all the text resources. Can one do this in Java/Android.

like image 651
fruehbier Avatar asked Jul 08 '26 16:07

fruehbier


1 Answers

Use the method getIdentifier(name, defType, defPackage) of the Resources class to get the id of a resource by name. Then you can do a normal getString(id) from the same class.

EDIT: a bit of Googling revealed this: this. You can find sample usage there.

like image 191
Savvas Dalkitsis Avatar answered Jul 11 '26 06:07

Savvas Dalkitsis