Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android dynamic String Resources

in my app I do a web request that returns some result code, e.g. 105. I have string resources that look like that

<string name="r105">O.K.</string>
<string name="r106">Something went wrong.</string>
<string name="r333">Fatal error.</string>

Now I want to do something like that

Toast.makeText(parent.getApplicationContext(),
        parent.getString(R.string.r+resultCode), Toast.LENGTH_LONG).show();

while r+resultCode is the resource identifier.

This does not work. Any idea how to do that?

like image 274
UpCat Avatar asked Dec 29 '11 12:12

UpCat


1 Answers

Try this getResources().getIdentifier(name, defType, defPackage) in a simple way.

Toast.makeText(this, getResources().getIdentifier("r"+resultcode, "string", 
getPackageName()), Toast.LENGTH_LONG).show();
like image 197
Lalit Poptani Avatar answered Sep 21 '22 20:09

Lalit Poptani