Using setImageDrawable dynamically to set image in an ImageView Using setImageDrawable dynamically to set image in an ImageView android android

Using setImageDrawable dynamically to set image in an ImageView


Try this,

int id = getResources().getIdentifier("yourpackagename:drawable/" + StringGenerated, null, null);

This will return the id of the drawable you want to access...then you can set the image in the imageview by doing the following

imageview.setImageResource(id);


Drawable image = ImageOperations(context,ed.toString(),"image.jpg");            ImageView imgView = new ImageView(context);            imgView = (ImageView)findViewById(R.id.image1);            imgView.setImageDrawable(image);

or

setImageDrawable(getResources().getDrawable(R.drawable.icon));


I personally prefer to use the method setImageResource() like this.

ImageView myImageView = (ImageView)findViewById(R.id.myImage);myImageView.setImageResource(R.drawable.icon);