Issue
I have to dynamically get some resources from the R file.
For example, let's say I have to dynamically generate ImageView
and dynamically get a drawable to put inside it.
I don't know how many ImageView
I have to generate; there may be 10, 50 or 100 so I have to do everything dynamically.
My main problem is to get dynamically the drawable from the R file.
Lets say I have this drawable: R.drawable.img1 R.drawable.img2 R.drawable.img3 R.drawable.img4
I should do something like this:
for(int i = 0; i < 10; i++){
ImageView iv = new ImageView(this);
iv.setImageResource(R.drawable.img + i);
}
How can I "build" this line of code: R.drawable.img + i
How can I reach my goal?
Solution
At first make sure your image is jpg
or png
You can try with this
for(int i = 0; i < 10; i++)
{
ImageView iv = new ImageView(this);
int imageResource = context.getResources().getIdentifier("@drawable/img "+i.replace(".jpg", ""), null,context.getPackageName());
iv.setImageResource(imageResource);
}
Answered By - IntelliJ Amiya
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.