In this post, you will learn how to perform an action using Button and onClick attribute in XML. It is simple and easy to understand. Before creating the project, Let’s know more about android:onClick attribute in Android.

How android:onClick XML attribute works here with Button?

how android onclick attribute works Simple Guide button onclick xml example in android-androidride.com

For detecting tap or button click event in android we can use android:onClick attribute. When users tap the views which hold android:onClick ,  android looks for the method in activity. If the exact method found, that will start execution. So, provide a valid method name that lives in hosted activity as the XML attribute value. Not only Button It works for ImageButton, CheckBox, and RadioButton too.

we can specify onClick attribute just like given below:

android:onClick=”Method_name_in_your_activity

For a better working of onClick attribute, you must take care these:

  1. The method name must be the same in value of onClick attribute and in Activity.
  2. The method has to accept a View parameter. The view here means which holds onClick xml attribute or which view was clicked. here, that is button
  3. it needs to return a void value.
  4. the method must be defined as public.

If you don’t obey above ones, most of the time you will end with an exception.

Let’s create a project, In this project, we will add a Button and TextView, When the user taps the Button it calls a method in the activity depends on android:onClick value and set text in TextView.

We assume that you opened Android Studio.

Step 1

File->New ->New Project

Application Name: OnClick XML Ex

Company Domain: androidride.com

click Next


Step 2

Select the form factors and minimum SDK

tick the phone and tablet checkbox and choose API 15: Android 4.0.3(IceCream Sandwich) and click Next.


Step 3

Select Empty Activity template

For this project, we just need an empty activity template. So just select the empty activity template and click Next.


Step 4

Create a new empty activity and layout

Next screen prompt for making an activity class file and its layout.

Activity Name: MainActivity

Tick the generate the layout checkbox if it’s not checked.

Layout Name: activity_main

Tick Backward Compatibility checkbox and Click finish.


Step 5

open activity_main.xml and paste the below code

You can drag LinearLayout, Button, and TextView wherever you want and give id, onClick attribute as you want or you can simply paste the below code. The layout just contains the Button and TextView inside LinearLayout.

Now you can see android:onClick value (myMethod) is underlined with red color and gives you an error message that “Corresponding method handler ‘public void myMethod(android.view.View) not found.

This is because we didn’t define the method in our MainActivity class.


Step 6
Define myMethod and set text

Obeying all rules specified earlier, create myMethod() and initialize and set the text to textview.

Just run your app.

screenshot button onclick xml example - Simple guide Button onclick xml example in android - androidride.com

For more information
Button google docs

Download Button OnClick XML Example
Please share this post and your valuable comments will help us to improve.