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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:50:21+00:00 2026-06-11T08:50:21+00:00

This is my first post, sorry for the title and the explication. Sorry for

  • 0

This is my first post, sorry for the title and the explication. Sorry for my English.

I define an .xml with all I need. I have 10 ImageView and the id for 10 ImageView are myimage01, myimage02, … , myimage010.
I need to select an image for an ImageView.
I can do it as follows:

String imageName=getImageName();      
  id = getResources().getIdentifier(imageName, "drawable", getPackageName());
  drawable = res.getDrawable(id);
  ImageView cant1= (ImageView)findViewById(R.id.myimage01);
  cant1.setImageDrawable(drawable);
  ImageView cant2= (ImageView)findViewById(R.id.myimage02);
  cant2.setImageDrawable(drawable);
  ImageView cant3= (ImageView)findViewById(R.id.myimage03);
  cant3.setImageDrawable(drawable);
  ImageView cant4= (ImageView)findViewById(R.id.myimage04);
  cant4.setImageDrawable(drawable); 
  ImageView cant5= (ImageView)findViewById(R.id.myimage05);
  cant5.setImageDrawable(drawable);
  ImageView cant6= (ImageView)findViewById(R.id.myimage06);
  cant6.setImageDrawable(drawable);
  ImageView cant7= (ImageView)findViewById(R.id.myimage07);
  cant7.setImageDrawable(drawable);
  ImageView cant8= (ImageView)findViewById(R.id.myimage08);
  cant8.setImageDrawable(drawable);
  ImageView cant9= (ImageView)findViewById(R.id.myimage09);
  cant9.setImageDrawable(drawable);
  ImageView cant10= (ImageView)findViewById(R.id.myimage010);
  cant10.setImageDrawable(drawable);

But this is too bad, its better to use a loop. But I don’t know do this.

I need something like that:

    String cad;
for(int i=0;i<10;i++){
      cad="myimage0";
      String cat= Integer.toString(i);
      cad=cad.concat(cat);
      ImageView cant1= (ImageView)findViewById(R.id.cad);
      cant1.setImageDrawable(drawable);
  }

But there is an error in:

ImageView cant1= (ImageView)findViewById(R.id.***cad***);

Thanks for all

  • 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-11T08:50:22+00:00Added an answer on June 11, 2026 at 8:50 am

    You can’t directly pass in a String like that to findViewById. You’ll need to look up the resource from your String first. Try this:

    int idResource = getResources().getIdentifier(cad, "id", getPackageName());
    ImageView cant1= (ImageView)findViewById(idResource);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is my first post here. I have a problem. I need to take
First of all Sorry for the really long post, now And this is my
First I am sorry about the length of this post, but I wanted to
This is my first post and I my first experience with jquery. I have
This is my first post, so please be nice :) I have a question
First of all, I am sorry if this question doesn't belong to SO since
First of all, sorry if I end up making a too long post, but
first of all this is my first post so maybe the whole description may
this is my first post. I have a huge problem which make me headaches.
(Sorry if the title is pretty useless) I have this function to get the

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.