How to set imageview resource from variable?
I have a lot of images (hundreds) and I need to put some of them as a resource of ImageView
, but if I would create If Then set Image to Image name, I die from these tons of code. I want to set Image resource which name is inside a variable, but I can't find out how. If I have
img.setImageResource(R.drawable.my_image);
How can I assign variable name at my_image
instead of true image name?
Please help.
Thanks.
Try this:
String resourceName = "clouds";
int resourceIdentifier = getResources().getIdentifier(resourceName, "drawable", Constants.CURRENT_CONTEXT.getPackageName());
Then
imgView.setImageResource(resourceIdentifier);