Buttons in Android help you to perform various events. You can switch between activities by adding a listener to your buttons or perform any action (by writing your business logic inside the onClick() method).  This Android button example teaches you to add buttons to your XML layout file and then you will learn to control buttons and perform various actions using java.

Below, you can find the actual screenshot of an Android button example application.

android button example

How to add buttons to the layout file.

You can use a XML layout file to add buttons. Below, you can find the code needed to add buttons to your activity.

Snippet 1:

Once you create a new Android project you can add the above lines of code inside your activity_main.xml file or inside a custom XML file you have created for your Android button application.

Now we are going to build the complete layout for our Android application. Let’s design our activity screen. Our activity screen consists of the following views:

  1. Three TextViews which will act as labels (“First Number:” , “Second Number: ” and “Total :”).
  2. Three EditTexts. Two of them will be used to take input from the user and the third one will be used to display the result when the user clicks on a button.
  3. One Button to handle the click event. On clicking the button, the sum of two numbers will get displayed.

Below, you can find the complete layout code for our Android button example.

Listing 1: create activity_main.xml layout file.

How to access Buttons in Android using java class?

The button class in Android can be imported by using the package below.

You can create a new button reference by using the code snippet below. We get the reference of a button by using its ID which we have created in our Android XML file as “R.id.button1“.

Handle the click event for an Android Button

Whenever the user clicks on an Android button, the application should respond to it. To do so in Android, we need to register a listener for it. So we make our Android activity implement the Onclicklistener interface.

Make sure you have imported the “android.view.View.OnClickListener;” interface. So now we can set a listener to Android buttons by using the line of code below. Next, we need to implement methods of the OnclickListener interface. It has the onClick() method which detects which button or view has been clicked. We will use the onclick method to implement our logic.

 You can add a listener to your Android button by using the line of code below.

Now let’s look at the main activity for our Android button example application.

Listing 1: create MainActivity.java file.

 How to detect which button has been clicked.

The onClick() method takes the view element which is being clicked as a reference. So in our Android button app example, when the user clicks on the addButton, the control will pass to the onClick() method and we fetch the view’s ID by using the getId() method. We can then compare it with the ID which is present in the Android XML layout file.

Here we have done the addition of two numbers inside our onClick method.

How to handle a click event when you have two or more buttons.

The onClick() method receives the reference of the view which gets clicked. So we can get the view ID from it and compare it with the ID present inside the XML layout file. In this way, we can handle a button click when one or more buttons are present in the Android activity.

What to learn next? 

You can learn the android array adapter. It will help you to get started with a listview.


In this Android button example, we learned about adding buttons to an Android activity. We also handled button click events. Now you will be able to fetch the ID of the clicked view so you can handle multiple buttons inside your Android activity.

If you have any suggestion or want to ask something, please do so in comments section below. Thank you for reading. All the best.