in my app I do a web request that returns some result code, e.g. 105. I have string resources that look like that
O.K.
A simple way to getting resource ID from string. Here resourceName is the name of resource ImageView in drawable folder which is included in XML file as well.Im getting "id" you can use "string".
int resID = getResources().getIdentifier(resourceName, "id", getPackageName());
ImageView im = (ImageView) findViewById(resID);
Context context = im.getContext();
int id = context.getResources().getIdentifier(resourceName, "drawable",
context.getPackageName());
im.setImageResource(id);
Try this getResources().getIdentifier(name, defType, defPackage)
in a simple way.
Toast.makeText(this, getResources().getIdentifier("r"+resultcode, "string",
getPackageName()), Toast.LENGTH_LONG).show();
Try as below, its working for me. Use parent.getApplicationContext()
for you.
String str = getString(R.string.r)+resultCode;
Toast.makeText(getApplicationContext(),
str, Toast.LENGTH_LONG).show();
You can do it using getResources().getIdentifier(name, defType, defPackage)
. Something like this:
// Assuming resultCode is an int, use %s for String
int id = getResources().getIdentifier(String.format("r%d", resultCode),
"string", getPackageName());
String result = getString(id);