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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T17:18:55+00:00 2026-06-09T17:18:55+00:00

I am self studying Android and now I am learning how to use buttons.

  • 0

I am self studying Android and now I am learning how to use buttons. I created a simple up counter which works like this:
I add the strings (eg. 1 2 3) in different text fields. Then I want to compaire those in pairs(1 with 2, 1 with 3, 2 with 3). The first string element is written on the first button, second on the second and after i press any of those buttons, the tags on the buttons has to change (if there was 1 and 2 so it should change to 1 and 3 or 2 and 3 etc) and the string element gets a higher rank. Everything seems to work well, but I think I am doing huge mistake with adding buttons. Can anyone help me? 🙂 Can I add button listeners like I did in code bellow? 🙂 Thank You!

public void counter() 
{
    int i = 0;
    int a = i + 1;

    for ( i = 0; i < candidates.size() - 1; i++ ) 
    {
            Log.d(TAG, "Setting button one tag: " + i );
            button_one.setTag(i);
            button_one.setText(candidates.get(i).name);

            for (a = i + 1; a < candidates.size(); a++)
            {
            Log.d(TAG, "Setting button two tag: " + a );
            button_two.setTag(a);
            button_two.setText(candidates.get(a).name);

            button_one.setOnClickListener(new View.OnClickListener() 
            {

                public void onClick(View view) {

                    /* Read the clicked tag */
                    int tag = (Integer) view.getTag();

                    /* Make higher rank */
                    candidates.get(tag).addRank();
                }
            });

            button_two.setOnClickListener(new View.OnClickListener() 
            {

                public void onClick(View view) {

                    /* Read the clicked tag */
                    int tag = (Integer) view.getTag();

                    /* Make higher rank */
                    candidates.get(tag).addRank();
                }
            }); 
            }       
    }
  • 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-09T17:18:56+00:00Added an answer on June 9, 2026 at 5:18 pm

    everything AndyRes said plus:
    you’re creating (candidates.size() * candidates.size()) number of OnClickListeners
    that’s A LOT of unnecessary objects hogging your memory.

    You should create ONE OnClickListener mListener = new View.OnClickListener etc... etc...
    and OUTSIDE the loop, probably during your OnCreate() just after you defined button_one = (Button)findViewById(R.id.button_one); you do button_one.setOnClickListener(mListener);

    that way you have only 1 listener that will be serving any amount of buttons you might have, and don’t need to be setting again the same listener.

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

Sidebar

Related Questions

I'm stuck with this. I'm self studying assenbler and translating some basics instructions. But
I'm studying for a M$ certificate. I use the 'self-paced' Training Kit (book). I
I am self studying C++ and I was thinking of a simple program to
hello i'm studying iOS programming i created a project, which is an empty application
I am self studying computer architecture offer at Michigan university. I do not understand
I am doing self-studying of operating systems. Tanenbaum in his book modern operating systems
self.scrollView.frame=CGRectMake(CGRectGetMinX(self.bounds),CGRectGetMaxY(self.bounds),CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds)); [self.scrollView addSubview:self.hourView]; [self.scrollView addSubview:self.gridView]; Now after embedding some views in the gridview, I
[[self enclosingScrollView] setHorizontalLineScroll:0]; the above api doesnt works. Help Appreciated...
I'm currently self-studying C# in my free time and thought of a little project
I'm currently self-studying C for mastering an university project. In that project we have

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.