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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:34:21+00:00 2026-05-26T04:34:21+00:00

Forgive me if this has been asked before. I have poured through this site

  • 0

Forgive me if this has been asked before. I have poured through this site and many others and can’t seem to find an answer. I have a working Android app but I’m trying to clean the code up. I have 3 buttons:

public Button button1, button2, button3;

Then, I have a method that “grabs” the clicks:

public void getButtonClick(View view)
{
 switch(view.getId())
 {
  case R.id.button1:
      // Do something button1 related here
  break;

  case R.id.button2:
      // Do something button2 related here
  break;

  case R.id.button3:
      // Do something button3 related here
 }
}

What I’d like to know is if there is any way I can have a generic method that will just wait for a button click and grab that variable so that I can avoid the multiple switch-case statements. Something on the line of:

public void oneMethodForAll(View view)
{
 clkdBtn = view.getButtonThatWasClicked();
 // Do stuff
}

Any help is greatly appreciated. TIA

  • 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-05-26T04:34:21+00:00Added an answer on May 26, 2026 at 4:34 am

    One thing you can do is to have the activity implement View.OnClickListener:

    public class FooActivity implements View.OnClickListener {
    
        @Override
        protected void onCreate(Bundle icicle) {
    
            Button btn1 = (Button) findViewById(R.id.button1);
            btn1.setOnClickListener(this);
    
            Button btn2 = (Button) findViewById(R.id.button2);
            btn2.setOnClickListener(this);
        }
    
        @Override
        public void onClick(View view) {
    
            switch (view.getId()) {
            case R.id.button1:
                // Button 1 was clicked
                break;
    
            case R.id.button2:
                // Button 2 was clicked
                break;
            }
        }
    }
    

    That will save you a little bit of typing since you’re not creating a bunch of anonymous click handlers, but you will still have to set the onClickListener of each button individually.

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

Sidebar

Related Questions

If this has been asked before please forgive me, I could not find it.
Forgive me if this has been asked before, I'm just unable to find an
Forgive me if this has already been asked, I couldn't find it. I have
Forgive me if this has been asked before, but I was unable to find
Forgive me if this has been asked before, but I assure you I've scoured
Forgive me if this has been asked before, I am sure it has but
Forgive me if this question has been asked before but I am new to
Forgive me if this has been asked repeatedly, but I couldn't find an example
Do forgive me if this has been asked before, I've been looking for the
This is my first question, so forgive me if it has been asked before.

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.