2

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.

OMG Ponies
  • 325,700
  • 82
  • 523
  • 502
fruehbier
  • 48
  • 1
  • 6

5 Answers5

2

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.

Savvas Dalkitsis
  • 11,476
  • 16
  • 65
  • 104
0

I had the same problem and I fixed it using this

okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring)

 String uriq="level"+level_num;
 level_res=getResources();
 int mystring=getResources().getIdentifier(uriq,"string",getPackageName());
 String level=level_res.getString(mystring);
Dante
  • 75
  • 1
  • 9
  • Can you explain your solution? – Michael Apr 02 '18 at 18:15
  • uriq represents the string that changes dynamically by channging level_num ,so all you need to do is use the method getIdentifier() to get the appropriate string – Dante Apr 02 '18 at 22:54
  • okey , whenever you want to access a string from strings.xml dynamically and what i mean by that is to avoid using getResources().getString(R.id.stringId) ,you create a string in which you can manipulate dynamically however you want in our case uriq ("stupid variable name") and then you create resource object which is in my example level_res and initialize it then you use this method called getIdentifier() which accepts your dynamic string as a parameter ,now u simply pass your ressource to the method getstring(mysttring) – Dante Apr 03 '18 at 13:04
  • Ezio please add that information to your answer, not in the comments :) – Michael Apr 03 '18 at 13:32
  • ah sorry , i thought you wanted explanation xD – Dante Apr 03 '18 at 13:37
0

Try: getResources().getString(R.id.stringId).

Mandel
  • 2,968
  • 2
  • 22
  • 19
0

All you have to do is call

this.getString(R.string.levelText_5)

If your in an area of the program in which you have access to a Context or Application, such as a ListAdapter call:

context.getString(R.string.levelText_5)

or

application.getString(R.string.levelText_5)

if you have no access to the context or application then call:

getResources().getString(R.String.levelText_5);

To do it dynamically call:

String name = "levelText_"+level;
int id = getIdentifier(name, "string", "com.test.mypackage");
getResources().getString(id);
Kenny
  • 5,522
  • 2
  • 18
  • 29
0

You should look at using getIdentifier(String, String, String) of the Resources class.

nicholas.hauschild
  • 42,483
  • 9
  • 127
  • 120