I have a var
with kind of food in it (For example: pizza, burger ext.), I want to use that var
to load a apecific image based on that var
value from my drawable
directory into the image view without using when
.
I have those files: pizza.png, burger.png
I want to use something like this:
var food = "pizza.png"
food_icon.setImageResource(R.drawable.food)//image is the pizza
food = "burger.png"
food_icon.setImageResource(R.drawable.food)//image is the burger
Is there a way to do this?
If I understand your question correctly you can use following method:
Resources resources = context.getResources();
final int resourceId = resources.getIdentifier("[your_drawable_name]", "drawable", context.getPackageName());
return resources.getDrawable(resourceId);
With this you can specify the name of your rawable and load it. If you need only the resourceId you can return the resourceId without getting the drawable itself.