I have to convert a string like "R.drawable.photo" into an integer that contain the same thing. How can I do?
<string name="id1">R.drawable.photo</string>
I need to get an ID that contain "R.drawable.photo"
I have to convert a string like "R.drawable.photo" into an integer that contain the same thing. How can I do?
<string name="id1">R.drawable.photo</string>
I need to get an ID that contain "R.drawable.photo"
Get it with the getIdentifier()
method:
int id = getResources().getIdentifier("photo", "drawable", getPackageName());
getResources()
and getPackageName()
may need a Context
if this code is not inside an activity:
int id = context.getResources().getIdentifier("photo", "drawable", context.getPackageName());
If the string has the R.drawable.
prefix, then:
String str = "R.drawable.photo"
int id = getResources().getIdentifier(str.replace("R.drawable.", ""), "drawable", getPackageName());
int resID = getResources().getIdentifier("myString",
"drawable", getPackageName());
replace myString
above with your value of string, which is "photo
"