2

I have 15 Images in my drawable folder and want 6 of these random images from this folder to be inserted into given locations when I open this page.
Any help will be greatly appreciated. Below is my xml

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_marginTop="30dp"
    android:layout_marginLeft="65dp"
    android:src="@drawable/circle" />

<ImageView
    android:id="@+id/imageView2"
    android:layout_width="60dp"
    android:layout_height="60dp"
    android:layout_alignParentRight="true"
    android:layout_alignTop="@+id/imageView1"
    android:layout_marginRight="65dp"
    android:src="@drawable/eclipse" />

<ImageView
    android:id="@+id/imageView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="52dp"
    android:src="@drawable/invertedtriangle" />

    <ImageView
    android:id="@+id/imageView4"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignRight="@+id/imageView2"
    android:layout_below="@+id/imageView1"
    android:layout_marginTop="52dp"
    android:src="@drawable/square" />

    <ImageView
    android:id="@+id/imageView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView1"
    android:layout_below="@+id/imageView3"
    android:layout_marginTop="52dp"
    android:src="@drawable/star" />

    <ImageView
    android:id="@+id/imageView6"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/imageView2"
    android:layout_below="@+id/imageView3"
    android:layout_marginTop="52dp"
    android:src="@drawable/polygon" />

 </RelativeLayout> 

Thanks.

  • what have you tried? post your xml layout, to show where the images should be positioned – Phantômaxx Feb 01 '14 at 16:40
  • all I have is i no this is no help to you but I really dont know where to start here – user2132102 Feb 01 '14 at 16:59
  • As far as I can see in this mess... Is that you have no container. Let me suggest you to use a RelativeLayout to contain your 6 ImageViews (which I don't see - I can only see a single ImageView). – Phantômaxx Feb 01 '14 at 17:02
  • I have a relative layout just cant fit it all in this comment box. In the layout is 6 different images similar to the one above so when i look at the graphical option there are 6 images on the screen. but i dont want these images to be the same everytime. Would I not need some way of randomly changing the images in my activity? – user2132102 Feb 01 '14 at 17:12
  • Edit your question to make the whole layout fit inside. Just add "My layout is:" and your layout. Yes, you do need a randomizer. But before, I have to see how the images are arranged. AND: do they all have to be the same? can someone be the same? are they to be ALL DIFFERENT from each other? (so this complicates the randomizer a bit, because you have to take in account the already used images) – Phantômaxx Feb 01 '14 at 17:15
  • OK, kind of a classical aligment: 3 on the left side each on top, and other 3 on the right side. Even if I had something to say about this (relativity of each ImageView, instead of a more standard anchoring to the parent - for the left/right alignments AND the marginTops seem a bit ECCESSIVE), but... OK, it's time to make some code. – Phantômaxx Feb 01 '14 at 17:24
  • Any suggestions how to do so? @Tobor – user2132102 Feb 01 '14 at 17:24

1 Answers1

0

Here you are:

First of, you need to import the namespace that contains the random object:

import java.util.Random;

Then you have to take an object out of it

final Random rnd = new Random();

Now, all the action happens in your onCreate method, after you set your contentView:

// Reference the images
ImageView img1 = (ImageView) findViewById(R.id.imageView1);
ImageView img2 = (ImageView) findViewById(R.id.imageView2);
ImageView img3 = (ImageView) findViewById(R.id.imageView3);
ImageView img4 = (ImageView) findViewById(R.id.imageView4);
ImageView img5 = (ImageView) findViewById(R.id.imageView5);
ImageView img6 = (ImageView) findViewById(R.id.imageView6);

// Your images are named img_0.png to img_14.png

// For image 1
final String str = "img_" + rnd.nextInt(14);
img1.setImageDrawable
(
    getResources().getDrawable(getResourceID(str, "drawable",
        getApplicationContext()))
);

// For image 2
str = "img_" + rnd.nextInt(14);
img2.setImageDrawable
(
    getResources().getDrawable(getResourceID(str, "drawable",
        getApplicationContext()))
);

// ...

Now, where is the trick?
In this method:

protected final static int getResourceID
(final String resName, final String resType, final Context ctx)
{
    final int ResourceID =
        ctx.getResources().getIdentifier(resName, resType,
            ctx.getApplicationInfo().packageName);
    if (ResourceID == 0)
    {
        throw new IllegalArgumentException
        (
            "No resource string found with name " + resName
            );
    }
    else
    {
        return ResourceID;
    }
}

I left out the lines of code for image 3 to 6, but they are the very same as for image 2.

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • ;) just keep in mind that this will possibly (well, probably) REPEAT the images. (Say, it will generate 0, 5, 7, 7, 7, 2). It doesn't take in account the already generated numbers. – Phantômaxx Feb 01 '14 at 18:26
  • Ya I noticed that. Is there an easy solution to that by any chance? – user2132102 Feb 01 '14 at 18:35
  • Yes. Check the accepted answer [here](http://stackoverflow.com/questions/5807512/android-generate-random-number-without-repetition). Basically, it stores the number in a list. Then, when it generates another number, it checks if it's already in the list. If so, it generates another number, until the generated number isn't present in the list. – Phantômaxx Feb 01 '14 at 18:43