Issue
I'm trying to dynamically construct the filename of an image in Android to use in the R.drawable. clause.
Here's my code:
imgHouse = (ImageView)findViewById(R.id.imageView);
strHouseNameImageFile = strHouseName + ".png";
imgHouse.setBackgroundResource(R.drawable.<filename>);
And in the last line above, I really want it to be a combination of the house name and .png but I'm not sure how to put a string together that will be used by R.drawable.
Could I do something like this for the last line, which now gives an error in Android Studio: imgHouse.setBackgroundResource(strHouseNameImageFiles);
Any help would be appreciated.
Thanks
Solution
int iconResId = getResources().getIdentifier(strHouseName, "drawable",getPackageName());
imgHouse.setImageResource(iconResId);
Answered By - Akash Singh
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.