@EboMike: I didn’t know that Resources.getIdentifier()
existed.
In my projects I used the following code to do that:
public static int getResId(String resName, Class<?> c) {
try {
Field idField = c.getDeclaredField(resName);
return idField.getInt(idField);
} catch (Exception e) {
e.printStackTrace();
return -1;
}
}
It would be used like this for getting the value of R.drawable.icon
resource integer value
int resID = getResId("icon", R.drawable.class); // or other resource class
I just found a blog post saying that Resources.getIdentifier()
is slower than using reflection like I did. Check it out.