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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T10:27:15+00:00 2026-05-15T10:27:15+00:00

I need a help with setting a random image using setImageResource method. In the

  • 0

I need a help with setting a random image using setImageResource method.
In the drawable folder, I have a jpeg file named photo0.jpg, photo1.jpg…photo99.jpg.
And the following code works:

int p = R.drawable.photo1;
image.setImageResource(p);

The above will display photo1.jpg but I want to show a random image.
I tried the following but it doesn’t work.

String a = "R.drawable.photo";
int n = (int) (Math.random()*100)
String b = Integer.toString(n);
String c = a+b;
int p = Integer.parseInt(c);//checkpoint
image.setImageResource(p);

It seems like the string “R.drawable.photoXX” isn’t being changed to integer at the checkpoint.
Could someone please teach me a right code?
Thank you in advance.

  • 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-15T10:27:16+00:00Added an answer on May 15, 2026 at 10:27 am

    Strings are pretty much evil when it comes to work like this due to the overhead costs. Since Android already provides you with integer id’s I would recommend storing all of them to an int array and then using a random number for the index.

    The code would look something like this:

    int imageArr[] = new int[NUM_IMAGES]; 
    
    imageArr[1] = R.drawable.photo;
    
    //(load your array here with the resource ids)
    
    int n = (int)Math.random()*NUM_IMAGES;
    
    image.setImage(imageArr[n]);
    

    Here we have a pretty straight forward implementation and bypass all the creation and destruction that occurs with the string concats.

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

Sidebar

Related Questions

now I need help with setting variable. I have two entities Season and Tournament,
I need some help setting up a particular terrain. I have a world that
I have made this chess game, but i need help with the accept setting
I need help setting up a simple C++/C# SWIG project. I am having a
Hi I need help with setting up the rails3-jquery-autocomplete gem with a manually assigned
I need a little help setting up a HTTP Post in C#. I appreciate
Need help with a query that I wrote: I have three tables Company id
Need help writing a script downloads data from google insight using c# this is
need help/guide for sql select query, I have 2 table stock and stock_history, in
I know this is not a programming question, and I need help with setting

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.