Issue
To make things simple: I got the drawable name from a JSON file and is now in a variable, how can I assign the correct drawable to an ImageView
with the name as a String
? We can assume that the drawable with that name always exists.
String image_name = "image1.png";
ImageView imageView = (ImageView)view.findViewById(R.id.myImageView);
imageView.setImageResource(image_name); // FAIL since is expecting the drawable and not the name as a String.
Solution
imageView.setImageResource(getDrawableFromName(context,"image1.png""));
public static int getDrawableFromName(Context context, String drawableName) {
return context.getResources().getIdentifier(drawableName, "drawable",
context.getPackageName());
}
Answered By - Jagadesh Seeram
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.