Handling Button click event

In android, we can handle the button click event in two ways:

  • We can handle the button click event using the layout xml file. In this file, we have to use the android:onClick attribute to define the method name. To handle the button click event, first we have to create one method and that method is defined in the xml file.
  • We have to create method in the java file as follows:
  • public void method_name(View view) { // code to perform action }
  • Now we have to call this method in the xml file using android:onClick attribute as follows:
  • android:onClick = “method_name”
  • When we click on this button then the method is called which is defined in the xml file.
  • Another way to handle the button click event by java code. Here we only use the java code and we does not use the android:onClick attribute means it is a runtime mechanism to handle the button click event.
  • For this purpose, we use setOnClickListener() method.
  • Below is the demo for the button click event:
  • android
    When app launched android
    When clicked on Using XML android
    when clicked on Using Java Code
  • The layout file is as follows:
  • <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    tools:context=".MainActivity" >

    android:text="@string/hello_world" />

    android:text="Using XML " />

    android:text="Using Java Code" />
  • In this file, we have called the xmlClick method using the android:onClick attribute and define a method in the java file.
  • The java code is shown below:
  • package com.example.buttondemo;

    import android.os.Bundle;
    import android.app.Activity;
    import android.view.Menu;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.TextView;

    public class MainActivity extends Activity {
    //declaration of Button and TextView object
    Button x,j;
    TextView message;

    protected void onCreate(Bundle savedInstanceState) {

    // register button and text view using id

    message = (TextView) findViewById(R.id.tvMessage);
    x = (Button) findViewById(R.id.btnXml);
    j = (Button) findViewById(R.id.btnJava);

    j.setOnClickListener(new OnClickListener() {

    public void onClick(View arg0) {
    // TODO Auto-generated method stub
    message.setText("Clicked using Java Code");

    public void xmlClick(View view)
    message.setText("Clicked using XML file");
    public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  • In this java file, first we have imported the following packages:
  • android
  • Android:widget.Button package is import to provide the functionality related to the button.
  • Other packages have been discussed in the TextView and EditText chapter.
  • Now we have to declare the Button object as follows:
  • android
  • Now we have to register the button and text view inside the onCreate() method as follows:
  • android
  • Now we have to provide the functionality to the button, first we will provide the facility for second button that is j as follows: android
  • In this we are using the setOnClickListener() method and we have passed an anonymous method to this function. It helps when we press the button then it will provide the indication to the system and the code will be executed at runtime.
  • In this method, we have to override the onClick() method that takes View type parameter to display the view on the screen.
  • Now we have used setText() method to set the text for TextView.
  • Now we have to specify the one method xmlClick() that takes one argument of View type.
  • Inside this method, we have set the text using the setText() method. It will display when we click on first button.
  • This method is as follows:
  • android
  • Now we have to define this method name in the xml file using the android:onClick attribute as follows:
  • android
  • Output of this demo is shown above.