Contents

Many Ways To Assign Click Event To Button On Android

 

1 Star2 Stars3 Stars4 Stars5 Stars (2 votes, average: 5.00 out of 5)
Loading...

 

Ho Ngoc Trang,  trangho214@gmail.com, is the author of this article and she contributes to RobustTechHouse Blog. 

 

Introduction

We can assign the click event to many different components such as Button, ListView, Dropbox and etc. When a component is assigned a click event, the component will do a specific task whenever it is clicked on. One of the most common components to assign click events to is a Button.

There are many tutorials to assign click events to buttons  but each of them has it’s own way to implement the click event. When I was a beginner Android programmer, I was very confused about which way is the right one, or why Tutorial A is different from Tutorial B when they might be doing the same thing.

In this article, I decided to list the four different ways I know to accomplish this.

 

1. Apply an onClickListener to the button in your activity

Declaring the OnClickListener within the call to the setOnClickListener method is common. A typical use of a push button in an activity is implemented like the following

main.xml

 <Button
        android:id="@+id/btn1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button1"/>

MainActivity Class

public class MainActivity extends Activity{
    Button button1;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //find view-elements
        button1 = (Button)findViewById(R.id.btn1);
        //Apply an instance of OnClickListener directly on  button,
        // When the button is clicked, the listener reacts and runs the code from onClick method.
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //TODO: do something here
                Toast.makeText(MainActivity.this,"Button1 is clicked", Toast.LENGTH_SHORT ).show();
            }
        });
    }

 

2. Define an onClick method in main.xml

Instead of applying an OnClickListener to the button in your activity, you can assign a method to your button in the XML layout, using the android:onClick attribute. In this attribute we specify the name of the method in Activity. This method will be invoked when the button is clicked.

main.xml

<Button
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:onClick="onClickButton2" 
    android:text="Button2"/>

Then, add this method inside Activity (MainActivity.java). Method requirements: public, void and receives a View object as a parameter:

MainActivity Class

public class MainActivity extends Activity{
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     //The View passed into the method is a reference to the widget that was clicked.
    public void onClickButton2(View v)
    {
        Toast.makeText(this,"Button2 is clicked", Toast.LENGTH_SHORT ).show();
    } 
}

 

 

3. Implement View.OnclickListener interface in the Activity

The Activity itself can implement the OnClickListener. Since the Activity object already exists this saves a small amount of memory by not requiring another object to host the onClick method.  To assign click event to button on Android, this frequently seems like the recommended way.

main.xml

<Button
        android:id="@+id/btn3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Button3"/>

MainActivity class

public class MainActivity extends Activity implements View.OnClickListener{
    Button button3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // find View-elements
        button3 = (Button)findViewById(R.id.btn3);
        // assign listener to button
        button3.setOnClickListener(this);
    }

    //Implement onClick method and tell it what to do when clicked.
    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.btn3)
                Toast.makeText(MainActivity.this,"Button3 is clicked", Toast.LENGTH_SHORT ).show();
}

 

 

4. Assign a variable of OnClickListener interface type to the button

In this way, we will declare a variable as a type of OnClickListener interface, then assigned it to the button.

main.xml

<Button
     android:id="@+id/btn4"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="Button4"/>

MainActivity class

public class MainActivity extends Activity{
    Button button4;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // find View-elements
        button4 = (Button)findViewById(R.id.btn4);
        // assign listener to button
        button4.setOnClickListener(onClickButton4);
    }

  View.OnClickListener onClickButton4 = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MainActivity.this,"Button4 is clicked", Toast.LENGTH_LONG ).show();
        }
    };
}

 

 

Conclusion

Many roads lead to Rome to assign click event to button on Android. Depending on your code layout, feel free to choose which of the above methods to use.

 

Brought to you by the RobustTechHouse team (A top iOS and Android developer in Singapore).  If you like our articles, please also check out our Facebook page.

 

Recommended Posts
Contact Us

We look forward to your messages. Please drop us a note for any enquiries and we'll get back to you, asap.

Not readable? Change text. captcha txt
Chargebacks2ecommerce4