Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8062355
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T10:38:27+00:00 2026-06-05T10:38:27+00:00

I have 16 imageviews on an activity……named img1->img16. I want a diff image on

  • 0

I have 16 imageviews on an activity……named img1->img16. I want a diff image on each image view and this cannot be hardcoded, depening on what number is passed as an extra, e.g. 1, 2 , 3….. the images added to these views must be different.

I currently have code that adds 16 images but these are not unique….. they are duplicated…

        int[] imageViews = {
            R.id.img1, R.id.img2,
            R.id.img3, R.id.img4,
            R.id.img5, R.id.img6,
            R.id.img7, R.id.img8,
            R.id.img9, R.id.img10,
            R.id.img11, R.id.img12,
            R.id.img13, R.id.img14,
            R.id.img15, R.id.img16
            };

    int[] images = {
            R.drawable.img1, R.drawable.img2,
            R.drawable.img3, R.drawable.img4,
            R.drawable.img5, R.drawable.img6,
            R.drawable.img7, R.drawable.img8,
            R.drawable.img9, R.drawable.img10,
            R.drawable.img11, R.drawable.img12,
            R.drawable.img13, R.drawable.img14,
            R.drawable.img15, R.drawable.img16
            };


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

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

And also, if possible, i need it so that the images are not hard coded in the images array as they will be different depending on what number has been passed.

So these images are for the number 1, but if number 2 was passed to the activity, the images would be different.

all the details are saved in a local database and the images are all in the drawable folder and will all follow the same naming styles e.g. img1 all the way to say img100….

Many thanks in advance.

/UPDATE/

Hi, I have tried that with the following code:

Random random = new Random(System.currentTimeMillis());
    List<Integer> generated = new ArrayList<Integer>();
        for(int v : imageViews) 
        {
             int next = random.nextInt(15) + 1;
             if (!generated.contains(next))
             {
                generated.add(next);
                ImageView iv = (ImageView)findViewById(v);
                iv.setImageResource(images[next]);
             }
        }

I may be doing it completely wrong there i guess…. but so far i have not found a duplicate, but does not help with the gaps, its as if it maybe generates 16 random ints but only adds the ones that are unique, rather than generating till reaches 16 unique images if you understand?

Any ideas on that front?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T10:38:29+00:00Added an answer on June 5, 2026 at 10:38 am

    Whenever you get your next random int, hold that value in an array. Then the next time you get a random int, check to see if that number has already been picked. If it has, repeat the process until a non-chosen int has been selected.

    Edit: Try this code:

    Random random = new Random( System.currentTimeMillis() );
    List<Integer> generated = new ArrayList<Integer>();
    for (int i = 0; i < imageViews.length; i++) {
    
        int v = imageViews[i];
        int next = random.nextInt( 15 ) + 1;
        if ( !generated.contains( next ) ) {
            generated.add( next );
            ImageView iv = (ImageView) findViewById( v );
            iv.setImageResource( images[next] );
        }
        else {
            i--;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 4 imageviews in an activity and I want that if the user
I have a activity where I have two imageViews. In onTouch I recognize which
I have a activity where I have multiple ImageViews and when you click on
I have a big problem. I have a activity where I have two imageViews.
I have imageView in activity. How I can set position this imageView in my
I have a activity where I have two imagesViews. This to imagesViews has onClickListener,
I have an imageview in listview in first activity, I want to send my
I have this method inside an Activity which implements an ImageLoader class: public void
I have a main activity with this layout file: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=http://schemas.android.com/apk/res/android
I have a very small activity that must show an image. If picture is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.