-3

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
  • 6,405
  • 6
  • 28
  • 69
justicepenny
  • 2,004
  • 2
  • 24
  • 48

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,618
  • 1
  • 11
  • 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,991
  • 11
  • 123
  • 154
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
  • 27,363
  • 32
  • 109
  • 125