view raw
Budda Budda - 1 year ago 33
Java Question

Android: how to handle button click

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.


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.


is in the form of an anonymous class.

Button clickButton = (Button) findViewById(;
clickButton.setOnClickListener( new OnClickListener() {

            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:


Say you have, 5 clickButtons:

Make sure your Activity/Fragment implement OnClickListener

// in OnCreate

Button mClickButton1 = (Button)findViewById(;
Button mClickButton2 = (Button)findViewById(;
Button mClickButton3 = (Button)findViewById(;
Button mClickButton4 = (Button)findViewById(;
Button mClickButton5 = (Button)findViewById(;

// somewhere else in your code

public void onClick(View v) {
    switch (v.getId()) {
        case {
            // do something for button 1 click

        case {
            // do something for button 2 click

        //.... 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.