3

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.

5 Answers 5

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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

2 Comments

Yes, but the question is how to get that dinamically? I would have to concatenate somehow.
Perhaps, you can have an array of strings in which case you need not concatenate.
0

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

Comments

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);

1 Comment

yeah, but 5 will come as a parameter, in a variable.
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);

5 Comments

Can you explain your solution?
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
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)
Ezio please add that information to your answer, not in the comments :)
ah sorry , i thought you wanted explanation xD

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.