3

How can I do something like this?

for(String name: nameArray){    
    ImageView i = new ImageView(this);   
// the line below does not work of course 
    i.setBackgroundImage(R.drawable.name);
//do some stuff
}

I've searched up some solutions but most of them involves integer IDs which I do not have

Scaraux
  • 3,397
  • 3
  • 35
  • 66
Pear
  • 635
  • 1
  • 10
  • 18

1 Answers1

2

Could you please check the code below?

I'm not totally sure if it works.. However, I know that you can use Resources.getIdentifier() which returns the ID. However, I'm not sure if it works with drawable as well.

for(String name: nameArray){    
    ImageView i = new ImageView(this);
    i.setBackgroundImage(getResources().getIdentifier(name, "drawable", getPackageName()));
}
W0rmH0le
  • 17,489
  • 8
  • 58
  • 72
  • This should work, yes, however, something may want to be done with that ImageView, otherwise only the last drawable will be shown – OneCricketeer Jun 02 '16 at 17:55