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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T11:30:34+00:00 2026-06-18T11:30:34+00:00

Having a solid experience in non-Java and non-Android area, I’m learning Android. I have

  • 0

Having a solid experience in non-Java and non-Android area, I’m learning Android.

I have a lot of confusion with different areas, one of them is how to handle button clicks. There are at least 4 way of doing that (!!!), they are briefly listed here

for consistency purpose I will list them:

  1. Have a member of the View.OnClickListener class in the activity and assign it to an instance that will handle onClick logic in the onCreate activity method.

  2. Create ‘onClickListener’ in the ‘onCreate’ activity method and assign it to the button using setOnClickListener

  3. Implement ‘onClickListener’ in activity itself and assign ‘this’ as a listener for the button. For the case if activity has few buttons, button id should be analyzed to execute ‘onClick’ handler for the proper button

  4. Have public method on the activity that implements ‘onClick’ logic and assign it to the button in the activity xml declaration

Question #1:

Are those all methods, is there any other option? (I don’t need any other, just curious)

For me, the most intuitive way would be the latest one: it requires the least amount of code to be typed and is the most readable (at least for me).

Though, I don’t see this approach used widely. What are cons for using it?

Question #2:

What are pros/cons for each of these methods? Please share either your experience or a good link.

Any feedback is welcome!

P.S. I’ve tried to Google and find something for this topic, but the only things I’ve found are description “how” to do that, not why is it good or bad.

  • 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-18T11:30:35+00:00Added an answer on June 18, 2026 at 11:30 am

    Question 1:
    Unfortunately the one in which you you say is most intuitive is the least used in Android. As I understand, you should separate your UI (XML) and computational functionality (Java Class Files). It also makes for easier debugging. It is actually a lot easier to read this way and think about Android imo.

    Question 2:
    I believe the two mainly used are #2 and #3. I will use a Button clickButton as an example.

    2

    is in the form of an anonymous class.

    Button clickButton = (Button) findViewById(R.id.clickButton);
    clickButton.setOnClickListener( new OnClickListener() {
                
                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    ***Do what you want with the click here***
                }
            });
    

    This is my favorite as it has the onClick method right next to where the button variable was set with the findViewById. It seems very neat and tidy that everything that deals with this clickButton Button View is located here.

    A con that my coworker comments, is that imagine you have many views that need onclick listener. You can see that your onCreate will get very long in length. So that why he likes to use:

    3

    Say you have, 5 clickButtons:

    Make sure your Activity/Fragment implement OnClickListener

    // in OnCreate
    
    Button mClickButton1 = (Button)findViewById(R.id.clickButton1);
    mClickButton1.setOnClickListener(this);
    Button mClickButton2 = (Button)findViewById(R.id.clickButton2);
    mClickButton2.setOnClickListener(this);
    Button mClickButton3 = (Button)findViewById(R.id.clickButton3);
    mClickButton3.setOnClickListener(this);
    Button mClickButton4 = (Button)findViewById(R.id.clickButton4);
    mClickButton4.setOnClickListener(this);
    Button mClickButton5 = (Button)findViewById(R.id.clickButton5);
    mClickButton5.setOnClickListener(this);
    
    
    // somewhere else in your code
    
    public void onClick(View v) {
        switch (v.getId()) {
            case  R.id.clickButton1: {
                // do something for button 1 click
                break;
            }
    
            case R.id.clickButton2: {
                // do something for button 2 click
                break;
            }
    
            //.... etc
        }
    }
    

    This way as my coworker explains is neater in his eyes, as all the onClick computation is handled in one place and not crowding the onCreate method. But the downside I see is, that the:

    1. views themselves,
    2. and any other object that might be located in onCreate used by the onClick method will have to be made into a field.

    Let me know if you would like more information. I didn’t answer your question fully because it is a pretty long question. And if I find some sites I will expand my answer, right now I’m just giving some experience.

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

Sidebar

Related Questions

so I am starting to learn how to develop Android Applications. I have experience
Having the SOLID principles and testability in mind, consider the following case: You have
I've been going at this problem for a solid couple hours now and having
Having searched a whole lot of similair posts, workarounds, I decided to make my
Having used storyboards for a while now I have found them extremely useful however,
Having a reallllll mind pain. I have a php image uploader which is all
Having a headache with IE. I have an image (24x24) which I'd like to
I'm trying to get into OOP lately, and I'm having trouble with SOLID principles
Coming from a .NET/C# Background and having solid exposure to PRISM, I really like
So having looked this up for a while now, I have read probably twenty

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.