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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:47:38+00:00 2026-05-21T03:47:38+00:00

I’m having trouble in making unrepeating random images on my Android App. Let’s say

  • 0

I’m having trouble in making unrepeating random images on my Android App.

Let’s say on my App I have this TOP 3 Sections, and I have 13 pictures. I need a method to generate 3 random images from the 13 pics on @drawable I have, but the 3 pictures should be different from one another.

For example: it can’t be img1,img1,img2 or img1,img1,img1 on the TOP 3 section but it should be img1,img2,img3.

So far, I have this method in hand:

final Button imgView = (Button)findViewById(R.id.top1);
        Random rand = new Random();            
        int rndInt = rand.nextInt(13) + 1; 
        String imgName = "img" + rndInt;
        int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
        imgView.setBackgroundResource(id);

On my layout, I specified one of the image ID on the TOP 3 section as top1. I still have the id top2, and top3 for the TOP 3 section. The above code will look up to my drawable images, which have the names ‘img1.jpg’, ‘img2.jpg’, ‘img3.jpg’ , ‘img4.jpg’, etc.

I’m open to any kind of solution. If somebody could improvise my code so 3 pictures won’t be the same with each other that would be perfect. But if any of you can offer a better solution with a new method, I’m open for it.

Thank you.

  • 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-21T03:47:39+00:00Added an answer on May 21, 2026 at 3:47 am

    I’d suggest you to store your image ids in List, use shuffle
    and retrieve the first element by removing.

    //the initial list of images
    final List<String> images = new ArrayList<String>();
    for (int i=1; i<=13; i++){
    images.add("img"+i);
    }
    //the method that retrieves random value
    Collections.shuffle(images, new Random());
    String randomImage = images.remove(0);
    

    EDIT:

    //this code should be inoked on every image add
    final Button imgView = (Button)findViewById(R.id.top1);
    Collections.shuffle(images, new Random());
    String imgName = images.remove(0);
    int id = getResources().getIdentifier(imgName, "drawable", getPackageName());  
    imgView.setBackgroundResource(id);
    

    The initialization of list should be performed once, outside of image add method (somewhere in onCreate probably.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
I have a bunch of posts stored in text files formatted in yaml/textile (from

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.