I am generating a string from database dynamically which has the same name of image in drawable folder.
Now I want to set that value for ImageView
btnImg.SetImageDrawable(GetDrawable(Resource.Drawable.button_round_green));
API 23 Android 6.0
The resource drawable names are not stored as strings, so you'll have to resolve the string into the integer constant generated during the build. You can use the Resources class to resolve the string into that integer.
Resources res = getResources();
int resourceId = res.getIdentifier(
generatedString, "drawable", getPackageName() );
imageView.setImageResource( resourceId );
This resolves your generated string into the integer that the ImageView can use to load the right image.
Alternately, you can use the id to load the Drawable manually and then set the image using that drawable instead of the resource ID.
Drawable drawable = res.getDrawable( resourceId );
imageView.setImageDrawable( drawable );
imageView.setImageDrawable(getResources().getDrawable(R.drawable.my_drawable));
From API 22 use:
Drawable myDrawable = ResourcesCompat.getDrawable(getResources(),
R.drawable.dos_red, null);
I personally prefer to use the method setImageResource() like this.
ImageView myImageView = (ImageView)findViewById(R.id.myImage);
myImageView.setImageResource(R.drawable.icon);