Issue
I parse image name by json, and now for displaying I would have to get the drawable id by the image name so I can do this:
background.setBackgroundResource(R.drawable.eventimage1);
When I get the image name the format is like this:
image_ev1.png
Solution
Use this function to get a drawable when you have the image name. Note that the image name does not include the file extension.
public static Drawable GetImage(Context c, String ImageName) {
return c.getResources().getDrawable(c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName()));
}
then just use setBackgroundDrawable
method.
If you only want the ID, leave out the getDrawable part i.e.
return c.getResources().getIdentifier(ImageName, "drawable", c.getPackageName());
Answered By - Kuffs
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.