How to add images dynamically to an ImageView
<p>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?</p>

<p>My code so far:</p>

<pre><code>    // 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 &lt; 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 &lt; sTiles.length; ++i) {
         ImageView image = (ImageView) findViewById(R.id.B0);
         image.setImageResource(R.drawable.a0);
    //}
</code></pre>

<p>--------------- layout/main.xml ------------</p>

<pre><code>&lt;TableRow&gt;
        &lt;ImageView
            android:id="@+id/A0"
            android:layout_column="1" /&gt;
        &lt;ImageView
            android:id="@+id/A1"
            android:gravity="right" /&gt;
    &lt;/TableRow&gt;
    &lt;TableRow&gt;
        &lt;ImageView
            android:id="@+id/B0" /&gt;
        &lt;ImageView
            android:id="@+id/B1" /&gt;
    &lt;/TableRow&gt;
</code></pre>

<p>Thanks, Sigurd!</p>
