Search code examples
androidandroid-image

Access a image dynamically from res folder android


I have a image in res folder like this

enter image description here

I want to access this one dynamically like this

holder.Viewcover.setImageDrawable(Drawable.createFromPath("R.id." + CoverimgUrl.get(position)));

CoverimgUrl is a list which have two image name one is book_cover & and another is blank_image this array is generated dynamically so how can I set this image from that list

In One word how to access a image dynamically which is in drawable folder and I need get that image name from an array list ?


Solution

  • Resources res = getResources();
    String mDrawableName = "image_name";
    int resourceId = res.getIdentifier(mDrawableName , "drawable", getPackageName());
    Drawable drawable = res.getDrawable(resourceId);
    icon.setImageDrawable(drawable );