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,841
  • 4
  • 40
  • 80
Pear
  • 785
  • 2
  • 10
  • 19

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()));
}
guipivoto
  • 18,327
  • 9
  • 60
  • 75
  • 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