0

I am creating a comic reader app that displays images and one a transparent button click moves to the next image I don't want to get into storage and or loading from the internet right now so I am keeping it simple and stupid(I know) and storing all the images in res/drawable there are over 1000 images and so loading them by name and typing them all out seems ridiculous they are labled t1 t2 t3 t... I am using

ImageView image = (ImageView) findViewById(R.id.MainComic);
image.setImageResource(R.drawable.t1);

in a switch that is uses and integer that is added to on the button click but that would require typing out 1000+ cases for the integers is there a way to have

image.setImageResource(R.drawable.t(int));

I know it is not that simple as that is what I tried first thanks in advance.

1 Answers1

0

You can use the Android Context :

ImageView image = (ImageView) findViewById(R.id.MainComic);   
Context context = image.getContext();
    int resId = context.getResources().getIdentifier("t1", "drawable", context.getPackageName());
    image.setImageResource(resId);

or you can use java instropection with Class.forname("...")

phico
  • 1,303
  • 11
  • 12