hemanth kumar hemanth kumar - 1 year ago 144
Android Question

How to set the image from drawable dynamically in android?

I am storing my project related images in drawable folder. Also I am storing the image names in string variable and dynamically I am trying to set those images to the imageview. But the image is not displaying. Please help me in this regard.

My Code:

int res = getResources().getIdentifier(imagename, "drawable", this.getPackageName());
imageview= (ImageView)findViewById(R.id.imageView);

In the above code "imagename" is the string variable which contains the image name.

Thanks in advance

Answer Source

Try this:

String uri = "@drawable/myresource";  // where myresource (without the extension) is the file

int imageResource = getResources().getIdentifier(uri, null, getPackageName());

imageview= (ImageView)findViewById(R.id.imageView);
Drawable res = getResources().getDrawable(imageResource);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download