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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T04:08:06+00:00 2026-06-07T04:08:06+00:00

i have 20 imageviews with background image set. When user clicks on one image

  • 0

i have 20 imageviews with background image set.

When user clicks on one image and after on a second one, i would like that the two

imageviews can swap their image background.

With the code i built, i got that ONLY the first image clicked updates his background.

private View z; //view for temporary save the first clicked image public ImageView[] images = new ImageView[20]; //array with my imageviews public void onClick(View v) { if (attivo == false) { attivo = true; z = v; } //save the clicked view to a temporary one else { //we enter here when we click on the second image //this cycles search inside the array which imageview //has the same ID of the saved one or the new one (the first/second image clicked) for (int h = 0; h<20; h++) { if (images[h].getId()== z.getId()) { images[h].setBackgroundDrawable(v.getBackground());} } for (int h = 0; h<20; h++) { if (images[h].getId()== v.getId()) { images[h].setBackgroundDrawable(z.getBackground());} } attivo = false; } }

The first cycle works! set the second background to the first imageview clicked.

The second cycle, even if the commands are executed, doesn’t work! the background is always the original one.

I know that i could do it in some different ways like use src instead of background, or

gridviews. But please i would like to know why this code doesn’t totally work.

  • 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-07T04:08:07+00:00Added an answer on June 7, 2026 at 4:08 am

    The best solution should be store in two variables your background resources:

    Drawable d1 = v1.getBackgound();
    Drawable d2 = v2.getBackgound();
    

    And then set your background drawable referencing to that variables:

    in one case:

     images[h].setBackgroundDrawable(d1);
     images[h].setBackgroundDrawable(d2);
    

    and viceversa:

     images[h].setBackgroundDrawable(d2);
     images[h].setBackgroundDrawable(d1);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

in my Home Screen i have set one image as background, And set Three
I have two ImageViews, one called imageView and the other called subView (which is
I have created one gridview and use custom adapter for that. I added two
I have a static background image (map) that loads in my ImageView. I have
I have a FrameLayout that consist some Image views & one EditText. I am
If i have an ImageView that fills the screen. The ImageView background is set
In my app i have to set up a small image as background, to
I have four tabs that hold four listviews, I want to set a background
I have following Image as src in ImageView. I use this as background image.
I have several ImageViews in a RelativeLayout. When the user taps any of 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.