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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T04:12:20+00:00 2026-06-05T04:12:20+00:00

Here i am creating 20 Button dynamically using for loop exmple for(int i =1

  • 0

Here i am creating 20 Button dynamically using for loop

exmple

for(int i =1 ;i <= 20 ;i++){
Button b = new Button(this);
b.setText(String.valueOf(i));
b.setId(String.valueOf(i));
b.setBackgroudColor(Color.Red);

b.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            b.setBackgroundColor(Color.GREEN);


        }
);

}

If i select 1st Button color will be changing to green remaining all are red. similarly if i select 2nd Button 1st and 2nd button color will be green and remaining all are red. this is the way it working fine .but my requirement is if i select the any button 2nd time all buttons and previous button which i pressed color should be red. to do that i am not getting the previous button id’s. can any help for this problem

  • 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-05T04:12:23+00:00Added an answer on June 5, 2026 at 4:12 am

    Hold a reference of the previously pressed button:

    final Button prevButton;
    for(int i =1 ;i <= 20 ;i++){
        Button b = new Button(this);    
        b.setText(String.valueOf(i));
        b.setId(String.valueOf(i));
        b.setBackgroudColor(Color.Red);
    
        b.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
    
                    if(prevButton != null)
                        prevButton.setBackgroundColor(Color.RED);
                    b.setBackgroundColor(Color.GREEN);
                    prevButton = (Button)v;
                }
        );
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm creating a button dynamically using JavaScript and at the same time assigning attributes
After dynamically creating an element with this: $('#btnAddBanner').click(function () { var uid = new
When creating buttons dynamically, i wished to have a new MouseEventHandler. I did this
I am creating a web application using zend, here I create an interface from
I am creating a installer using WIX, The requirement here is that when the
I'm new to iPhone dev. My app is dynamically creating buttons, following from my
First off, I am not dynamically creating any controls. This is the order I
I am creating an iPad app, in this I am dynamically creating the interface
I have this form Im creating and when you click on the Next button
Using mootools I have a 'builder' class that manufactures form objects, dynamically creating divs

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.