Is it possible to access R.drawable variables dynamically?

时间:2017-08-05 11:10:36

标签: java android resources

Let's say I want to access to my image : R.drawable.character

I have String drawableName = character

Is there a way to do something like that or it is forbidden in Java =>

R.drawable.drawableName to access R.drawable.character ?

I found this but it is for sql database : How to get the image from drawable folder in android?

1 个答案:

答案 0 :(得分:1)

int id = getResources().getIdentifier(drawableName , type, package);

This will get you the ID of the resource you are looking for. With it, you can then access the resource from the R class.