Sigurd
Sigurd

Reputation: 25

How to add images dynamically to an ImageView

I'm writing a puzzle game to learn developing Android Apps. But now I'm stuck on how to add images dynamically to the ImageViews in the layout file (see below). I'm trying to make a loop where I add a random image to each of the ImageViews. But I can't find any examples on how to do this. My images are named the same as the ImageViews, only in lower case letters. Or are there some other and better ways to solve this?

My code so far:

    // Puzzle of 2x2 tiles
    String[] sTiles = {"A0","A1","B0","B1"};
    final Random myRandom = new Random();

    // Random tiles
    String tmp = null;
    for (int i = 0; i < sTiles.length; ++i) {
        tmp = sTiles[i];
        int r = myRandom.nextInt(sTiles.length);
        sTiles[i] = sTiles[r];
        sTiles[r] = tmp;
    }

    // Lopp to add images randomly to the screen (layout/main.xml)
    //for(i=0; i < sTiles.length; ++i) {
         ImageView image = (ImageView) findViewById(R.id.B0);
         image.setImageResource(R.drawable.a0);
    //}

--------------- layout/main.xml ------------

<TableRow>
        <ImageView
            android:id="@+id/A0"
            android:layout_column="1" />
        <ImageView
            android:id="@+id/A1"
            android:gravity="right" />
    </TableRow>
    <TableRow>
        <ImageView
            android:id="@+id/B0" />
        <ImageView
            android:id="@+id/B1" />
    </TableRow>

Thanks, Sigurd!

Upvotes: 2

Views: 17189

Answers (3)

Kevin Coppock
Kevin Coppock

Reputation: 134664

Try setting up a few int arrays to store your drawable and widget IDs. It's faster than using reflection to find them by name. Something like this should do the trick:

int[] imageViews = {
    R.id.A0, R.id.A1,
    R.id.B0, R.id.B1,
    R.id.C0, R.id.C1 //...
    };

int[] images = {
    R.drawable.a0, R.drawable.a1,
    R.drawable.b0, R.drawable.b1,
    R.drawable.c0, R.drawable.c1 //...
    };

Random random = new Random(System.currentTimeMillis());

for(int v : imageViews) {
    ImageView iv = (ImageView)findViewById(v);
    iv.setImageResource(images[random.nextInt(images.length - 1)]);
}

You may want to add some special handling if you want to ensure that all the ImageViews get unique images, but that should work as is.

Upvotes: 5

ninjasense
ninjasense

Reputation: 13846

You'll have to make some sort of container(Linear Or Relative layout possibly) and add the images to the view. Example:

   RelativeLayout cbrl = new RelativeLayout(this);
            RelativeLayout.LayoutParams cbp = new RelativeLayout.LayoutParams(
                        LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
                cbVal.setLayoutParams(cbp);

    for(i=0; i < sTiles.length; ++i) {
         ImageView image = (ImageView) findViewById(this);
         image.setImageResource(R.drawable.xX);
cbrl.addView(iv);
    }

Upvotes: 0

Marcos Vasconcelos
Marcos Vasconcelos

Reputation: 18276

An solution is read with reflection the values from R.drawable and load it as Bitmap by getting the value of the field in R.

It's something like:

    public static void example() throws IllegalArgumentException, IllegalAccessException{
        Class<R.drawable> clazz = R.drawable.class;
        for(Field field : clazz.getDeclaredFields()){
            Bitmap bmp = BitmapFactory.decodeResource(getResources(), (Integer) field.get(null));
            //bmp is your image
        }
    }

Upvotes: 0

Related Questions