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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T05:59:21+00:00 2026-06-05T05:59:21+00:00

I have written this code for a dynamic layout where I am using this

  • 0

I have written this code for a dynamic layout where I am using this loop to generate a pair of buttons (this is the part of code where I generate them)

  for(int i = 1; i <= 2 ; i++) {
        Button button1 = new Button(this);
        button1.setTag("age");
        button1.setId(i);
        layout.addView(button1);

        Button button2 = new Button(this);
        button2.setId(i);
        button2.setTag("country");
        button2.setEnabled(false);
        layout.addView(button2);

        button1.setOnClickListener(this);
        button2.setOnClickListener(this);
       }

What I wish to do is if button1 is clicked, button2 should get enabled (initially it is disabled).

This would be a very easy task to do if the buttons were created in xml as then they will have separate R.id.xxxxx names for each, but here I am unable to understand how to detect the other button in the OnClick(View v) method so that I can change if it is enabled or not, I have tried to add the tag for each button so that I have another parameter to recognize the buttons but I have no idea how to recognize the other button with the view information of the clicked button1.

  • 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-05T05:59:22+00:00Added an answer on June 5, 2026 at 5:59 am

    I assume that you are using the button tags in your click processing. To keep the tag data and add the needed wiring between buttons, you can create a data structure that would serve as a tag:

    static class ButtonTag {
        String buttonType;
        Button partner;
        ButtonTag(String type, Button button) {
            buttonType = type;
            partner = button;
        }
    }
    

    Then you could reorganize your setup code:

    for(int i = 1; i <= 2 ; i++) {
        Button button1 = new Button(this);
        button1.setId(i);
        layout.addView(button1);
    
        Button button2 = new Button(this);
        button2.setId(i);
        button2.setEnabled(false);
        button1.setTag(new ButtonTag("age", button2));
        button2.setTag(new ButtonTag("country", button1));
        layout.addView(button2);
    }
    

    The click processing will obviously need to be changed to cast getTag() to a ButtonTag instead of a String.

    If you don’t need the “age” and “country” information to distinguish button types, just set each button as the tag for the other.

    EDIT:

    With the latter scheme, here’s how you would use this in a click listener:

    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof Button) {
            Button btn = (Button) tag;
            btn.setEnabled(true);
            v.setEnabled(false);
        }
    }
    

    If you needed the “age” and “country” part of the tag for other reasons, the code would be only a little different:

    public void onClick(View v) {
        Object tag = v.getTag();
        if (tag instanceof ButtonTag) {
            ButtonTag bTag = (ButtonTag) tag;
            bTag.partner.setEnabled(true);
            v.setEnabled(false);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this code written in .NET 4.0 using VS2010 Ultimate Beta 2: smtpClient.Send(mailMsg);
Hi I have written code like this @Id @Column(nullable=false) @GeneratedValue(strategy=GenerationType.AUTO) public int getUserID() {
I have written this code for a search page in PHP and I would
I have written this code to join ArrayList elements: Can it be optimized more?
I have written this code to convert string in such format 0(532) 222 22
I have written this code in javascript however I have to make it work
So I have written this code here: highlighter: function (item) { var parts =
I am a beginner in Python and I have written this simple code but
I have this code I have written to make my life easier when downloading
In my code I have written this but it fails to compile: In Class1.h:

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.