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

  • Home
  • SEARCH
  • 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 3997950
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T07:27:53+00:00 2026-05-20T07:27:53+00:00

I’m writing a puzzle game to learn developing Android Apps. But now I’m stuck

  • 0

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?

My code so far:

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

————— layout/main.xml ————

<TableRow>
        <ImageView
            android:id="@+id/A0"
            android:layout_column="1" />
        <ImageView
            android:id="@+id/A1"
            android:gravity="right" />
    </TableRow>
    <TableRow>
        <ImageView
            android:id="@+id/B0" />
        <ImageView
            android:id="@+id/B1" />
    </TableRow>

Thanks, Sigurd!

  • 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-20T07:27:53+00:00Added an answer on May 20, 2026 at 7:27 am

    Try setting up a few int arrays to store your drawable and widget IDs. It’s faster than using reflection to find them by name. Something like this should do the trick:

    int[] imageViews = {
        R.id.A0, R.id.A1,
        R.id.B0, R.id.B1,
        R.id.C0, R.id.C1 //...
        };
    
    int[] images = {
        R.drawable.a0, R.drawable.a1,
        R.drawable.b0, R.drawable.b1,
        R.drawable.c0, R.drawable.c1 //...
        };
    
    Random random = new Random(System.currentTimeMillis());
    
    for(int v : imageViews) {
        ImageView iv = (ImageView)findViewById(v);
        iv.setImageResource(images[random.nextInt(images.length - 1)]);
    }
    

    You may want to add some special handling if you want to ensure that all the ImageViews get unique images, but that should work as is.

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

Sidebar

Related Questions

No related questions found

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.