Onclick in XML layout

When the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method.

Note: If you use this event handler in your code, make sure that you have that button in your MainActivity. It won’t work if you use this event handler in a fragment because the onClick attribute only works in Activity or MainActivity.

Example:

XML Button:

<Button xmlns:android="http:// schemas.android.com/apk/res/android"
android:id="@+id/button_sead"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/button_send"
android:onClick="sendMessage"
/>

In MainActivity class

/** Called when the user touches the button */
public void sendMessage(View view)
{
// Do something in response to button click
}

Example Implementation of the above Method:

In this Example on Clicking the button the text will be displayed and we are using an extra empty text TextView created beforehand:

MainActivity.java
package com.example.handling_button_java;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
import androidx.core.view.WindowInsetsCompat;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void sendMessage(View view){
        // All the Operations can be done Here.
        final TextView text=findViewById(R.id.text_after);
        text.setText("This is the after Result");
    }
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/button_send"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button"
        tools:layout_editor_absoluteX="160dp"
        tools:layout_editor_absoluteY="44dp"
        android:onClick="sendMessage"
        android:layout_gravity="center"
        />

    <TextView
        android:id="@+id/text_after"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Make sure that your sendMessage method should have the following :

  • Be public
  • Return void
  • Define a View as its only parameter (this will be the View that was clicked)

Handling Click Events in Button in Java Android

Click Events are one of the basic operations often used in Java Android Development to create Java Android Applications. In this article, we will learn about how to Handle Click Events in Button in Android Java.

Similar Reads

Methods to Handle Click Events in a Button

There are 2 ways to handle the click event in the button...

Onclick in XML layout

When the user clicks a button, the Button object receives an on-click event. To make click event work add android:onClick attribute to the Button element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity hosting the layout must then implement the corresponding method....

Using an OnClickListener

You can also declare the click event handler programmatically rather than in an XML layout. This event handler code is mostly preferred because it can be used in both Activities and Fragments....

Contact Us