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 6579277
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:52:41+00:00 2026-05-25T15:52:41+00:00

i am using 9 image view’s i want set images to imageview randomly ,

  • 0

i am using 9 image view’s i want set images to imageview randomly , when I click on refresh button, but I tried like this it’s working for random allocation of images but it’s repeating the same image in two (or) three imageview’s at a time. where is the problem in my code..

 final int[] imageViews = {
            R.id.imgview11, R.id.imgview12, R.id.imgview13, 
            R.id.imgview21, R.id.imgview22, R.id.imgview23, 
            R.id.imgview31, R.id.imgview32, R.id.imgview33 };

    final int[] images = {
            R.drawable.i1, R.drawable.i2, R.drawable.i3, 
            R.drawable.i4, R.drawable.i5, R.drawable.i6, 
            R.drawable.i7, R.drawable.i8, R.drawable.empty };

    final ImageButton shuffle = (ImageButton) findViewById(R.id.new_puzzle); 
    shuffle.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {        
            Random generator = new Random();
            //int n = 9;
            //n = generator.nextInt(n);
            //Random random = new Random(System.currentTimeMillis());
            for(int v : imageViews) {
                ImageView iv = (ImageView)findViewById(v);
                iv.setImageResource(images[generator.nextInt(images.length - 1)]);
            }
        }
    });      

i don’t want repeat, one image for one imageview only..

  • 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-05-25T15:52:42+00:00Added an answer on May 25, 2026 at 3:52 pm

    using the post of blessenm ,i wrote a similar code that you need. check if this helps you.

    shuffle.setOnClickListener(new View.OnClickListener() {
                public void onClick(View view) { 
    
                Random rng = new Random(); 
                List<Integer> generated = new ArrayList<Integer>();
                for (int i = 0; i < 9; i++)
                {
                  while(true)
                  {
                     Integer next = rng.nextInt(9) ;
                     if (!generated.contains(next))
                     {
                        generated.add(next);
                        ImageView iv = (ImageView)findViewById(imageViews[i]);
                        iv.setImageResource(images[next]);
                        break;
                     }
                   }
                }
                }
            });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to change the image in image view on button click but it
I am using 5 label,1 button,1 image view in custom cells of table view.but
I want to set text field , uibutton, label and image view at the
I'm using the following code to load an image in an image view. But
I am using SDWebImage to load images into my table view. I would like
I am using an Image view with these XML attributes <ImageView android:id=@+id/widget_list_bitmap android:layout_width=wrap_content android:layout_height=wrap_content
I want to display the compass(Image View) on device at a specific location.I tried
I am trying to set the background color of an image view. <ImageView android:id=@+id/my_image
I am using a webview and on that web view i have a image.
i can't display the image on my aspx view.. i'm using mysql as database

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.