Sanchez Tanguy
Sanchez Tanguy

Reputation: 51

Is it possible to access R.drawable variables dynamically?

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?

Upvotes: 1

Views: 1110

Answers (1)

MohammadReza Eram
MohammadReza Eram

Reputation: 568

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.

Upvotes: 2

Related Questions