-2

I want to set image dynamically on the ImageButton in android. However, I have more than 200 images. What would be the good solution for that?

The best idea is that I can use the name of image to call different images. i.e. imagebutton.setImage("/res/abc.png"); however, it seems to me that it is not trivial to do so.. please help me to solve these problems.

Nimantha
  • 5,793
  • 5
  • 23
  • 56
justicepenny
  • 1,979
  • 2
  • 23
  • 46

3 Answers3

1

You can take the images in your resource folder. After that follow this simple code:

try {               
                Class<drawable> res = R.drawable.class;
                if(str!=null){                                   
                Field field = res.getField(str);
                int drawableId = field.getInt(null);
                bengalidaypng.setImageResource(drawableId);              
                }
            }
            catch (Exception e) {
                      System.out.println("Image not found in drawable folder");
            } 

A more detailed sample can be found here.

Aayush Kumar
  • 1,598
  • 1
  • 10
  • 31
1

You could use Typed Array resource. There is an example at the end of the link how to use it for drawables (images).

Edited:

Resources can be accessed as raw data: use AssetManager.open(..) Then you can use BitmapFactory.decodeStream(..) to create a Bitmap from the data stream.

Peter Knego
  • 79,504
  • 10
  • 122
  • 151
0

You could store the images into a database, then pull the images into the ListView when you are binding each row.

Community
  • 1
  • 1
Bryan Denny
  • 26,977
  • 32
  • 105
  • 125