Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle a button being clicked in Android?

Tags:

android

In Android there seems to be 3 common ways of handling button clicks, how much difference is there between the methods? And are any of them 'better' in some way?

The three methods I keep seeing are:

Anonymous class

Find the button by it's ID, then pass a new anonymous class to setOnClickListener, e.g. in onCreate

findViewById(R.id.myButton).setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        // .. Whatever
    }
});

Implement OnClickListener

Implement OnClickListener and pass this to setOnClickListener, then use a switch statment based on the button ID, e.g. in onCreate

findViewById(R.id.myButton).setOnClickListener(this);

and implement onClick like

public void onClick(View v) {
    switch(v.getId()) {
        case R.id.myButton:
            // ... whatever ...
            break;
    }
}

Use onClick XML atribute

In the XML layout for your activity, instead of giving your button an ID, use onClick like this:

<Button 
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:onClick="buttonClicked" 
    android:text="Button" />

Then have a buttonClicked method in your Acitiviy like this:

public void buttonClicked(View v) {
    // ... whatever ...
}

At the moment I tend to use the XML attribute, but that's just because it involves the least amount of code. When should I use the other methods?

like image 277
Wilka Avatar asked Aug 16 '11 17:08

Wilka


People also ask

How does android handle multiple clicks?

The actual solution to this problem is to use setEnabled(false) which greys out the button, and setClickable(false) which makes it so the second click can not be received I have tested this and it seem to be very effective.

Is a method that is invoked each time the user clicks or taps on a clickable UI element such as a button?

A click handler is a method that is invoked when the user clicks on a user interface element.

Is onClick deprecated in android?

onClick is prompted deprecated.


2 Answers

The first two are the classic approaches. Which one you prefer is more of a general Java question than an Android question. The third one was added later to make things easier.

Setting up a click listener on a button is very common task, but it requires quite a bit of boilerplate code. One way to reduce the amount of boilerplate is to share a single click listener between several buttons. While this technique reduces the number of classes, it still requires a fair amount of code and it still requires giving each button an id in your XML layout file. With Android 1.6, none of this is necessary. All you have to do is declare a public method in your Activity to handle the click (the method must have one View argument)

Source

like image 72
mibollma Avatar answered Nov 06 '22 07:11

mibollma


I've really always seen it as preference. I'm not sure there's any performance advantage to either other than the last two methods may be slightly faster since they're not creating objects at runtime.

The first option isolates the code to the single button so it's very easy to debug since you know only that code will be executed when that button is clicked. However, many buttons can cause initialization methods to expand to large sizes.

The last two methods put all button handling in one place which can be convenient and cleaner at times, but with many buttons you have to decipher which button was tapped by the user via the v.getId() method.

The last option allows you to easily specify specific methods for specific buttons so you can separate them out like that, but again you'll have many methods used for single purposes.

I've always used the inline method (anonymous class) for custom dialog windows that have buttons since it keeps the code with the rest of the dialog rather than in an activity or class. I just initialize the buttons of the custom dialog when I override the onCreateDialog.

I implement the OnClickListener on the Activity if the buttons are on the main window.

like image 26
DeeV Avatar answered Nov 06 '22 08:11

DeeV