-2

I have +300 png pictures.Their names are like; aaa.png abka.png bxja.png daw.png.

According to my setImage(String pictureName) function i want to set imageview View resource dynamically.

private void setImage(String pictureName){
      myImgView.setBackgroundResource(R.drawable + picturename);
}

My main idea is like this but as you can see it is imposible. What did i think to solve this problem is creating switch statemant for all +300 images but it will be really huge effort to implement this.

What am i asking is; is there a any easy way to implement this.

M. Witney
  • 45
  • 3
  • 12

2 Answers2

0

You should use this code, assuming that the function setImage is a member of activity

private void setImage(String pictureName)
{
     int id = getResources().getIdentifier(pictureName,"drawable",getPackageName());
     myImgView.setBackgroundResource(id);
}
regev avraham
  • 1,102
  • 1
  • 9
  • 24
0

R.drawable + picturename won't work because R.drawable.picturename is actually an integer that references your resource.

However, you can use Resources.getIdentifier to get that integer.

Then you can call setBackgroundResource() with the output

ACVM
  • 1,497
  • 8
  • 14