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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:52:44+00:00 2026-06-05T07:52:44+00:00

I am trying to write a loop to set the resources of 5 dice.

  • 0

I am trying to write a loop to set the resources of 5 dice. If I change the “imgBtnName in the last line of the code to any of the dice names (such as “dice1”), the line doesn’t error. However, when I try to concatenate the name to fit the for loop, the following error notification on the setImageesource:

error

The method setImageResource(int) is undefined for the type String

Any ideas? I feel like I may be missing a syntax similar to the “int id” line.

code

public void PlayGame()  
    {  
        dice1 = (ImageButton)findViewById(R.id.btndice1);  
        dice2 = (ImageButton)findViewById(R.id.btndice2); 
        dice3 = (ImageButton)findViewById(R.id.btndice3); 
        dice4 = (ImageButton)findViewById(R.id.btndice4); 
        dice5 = (ImageButton)findViewById(R.id.btndice5);



        begin = (Button)findViewById(R.id.btnroll);
        roll = (Button)findViewById(R.id.btnbegin);

        begin.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Random rand = new Random();     

                for (int i = 0; i < 6; i = i + 1) {
                    int rndInt = rand.nextInt(6) + 1; // Random number between 1 and 6                  
                    String imgBtnName = "dice" + (i + 1);
                    String imgName = "die" + rndInt;                    
                    int id = getResources().getIdentifier(imgName, "drawable", getPackageName());

                    imgBtnName.setImageResource(id);  //trying use the imgButnName string to set dice1, dice2, dice3 etc to set the imagebutton resource
                }



            }
        });  
    }  
  • 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-05T07:52:46+00:00Added an answer on June 5, 2026 at 7:52 am

    Wrap your ImageViews in an arry and loop around them

        dice1 = (ImageButton)findViewById(R.id.btndice1);  
        dice2 = (ImageButton)findViewById(R.id.btndice2); 
        dice3 = (ImageButton)findViewById(R.id.btndice3); 
        dice4 = (ImageButton)findViewById(R.id.btndice4); 
        dice5 = (ImageButton)findViewById(R.id.btndice5);
    
        ImageButton[] dice = new ImageButton[5]{dice1, dice2, dice3, dice4, dice5};
    
        for (int i = 0; i < dice.length; i++) {
             int id = R.drawable.ic_launcher;
             dice[i].setImageResource(id);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to use openmp to multithread a loop through std::set. When I write
I'm trying to write one line of code to tell me if there is
I'm trying to write a unit test that will loop through all action methods
Trying to write a code at the moment that basically tests to see if
I'm trying to write some jQuery code that gets the combined width of images
I'm trying to write some microcontroller code using Texas Instruments examples, and it uses
I'm trying to write a SSL-based async server using Boost ASIO example code from
I'm trying to write a simple generic parallel code for minimizing a function in
I know this code is correct, I did not write it. I am trying
I am trying write a function that generates simulated data but if the simulated

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.