How to Use OnClick Method in Android

Introduction

In this article I will tell you about the "onClick()" method that is available in the event listener interface. This method will be called by the Android framework to see that the listener that has been registered is triggered by user interaction with the item in the UI. When we want to use many buttons or views in our project can use the "android:onClick="oncClick"" attribute in XML file for every view. And in the Java file we can use "onClick( View view)" on every view without set "onclickLitener" using get id of every button or view, as described in the following.

Step 1

Create new a project as "File" -> "New" -> "Android Application Project" as shown below:

newandroid.jpg

Step 2

Now open the XML file as "res/layout/activity_main.xml" and update it as in the following code.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"

    xmlns:tools="http://schemas.android.com/tools"

    android:layout_width="match_parent"

    android:layout_height="match_parent"

    tools:context=".MainActivity" >

    <Button

        android:id="@+id/button1"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_alignParentTop="true"

        android:onClick="onClick"

        android:text="Button 1" />

    <Button

        android:id="@+id/button2"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/button1"

        android:layout_marginTop="36dp"

        android:onClick="onClick"

        android:text="Button 2 " />

    <Button

        android:id="@+id/button3"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/button2"

        android:layout_marginTop="45dp"

        android:onClick="onClick"

        android:text="Button 3" />

    <Button

        android:id="@+id/button4"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/button3"

        android:layout_marginTop="51dp"

        android:onClick="onClick"

        android:text="Button 4" />

    <Button

        android:id="@+id/button5"

        android:layout_width="wrap_content"

        android:layout_height="wrap_content"

        android:layout_alignParentLeft="true"

        android:layout_alignParentRight="true"

        android:layout_below="@+id/button4"

        android:layout_marginTop="38dp"

        android:onClick="onClick"

        android:text="Quit App" />

</RelativeLayout>

Step 3

Open Java from "src/com.newandroid.project/MainActivity.java" and update it with the following code:

package com.newandroid.project;

import android.os.Bundle;

import android.app.Activity;

import android.view.Menu;

import android.view.View;

import android.widget.Button;

public class MainActivity extends Activity {

      Button button1,button2,button3,button4,button5;

    @Override

    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);

        button1=(Button) findViewById(R.id.button1);

        button2=(Button) findViewById(R.id.button2);

        button3=(Button) findViewById(R.id.button3);

        button4=(Button) findViewById(R.id.button4);

        button5=(Button) findViewById(R.id.button5);

    }

    public void onClick(View v) {

        final int id = v.getId();

        switch (id) {

        case R.id.button1:  

           button1.setText("You clicked on Button 1");       

            break;

        case R.id.button2:       

            button2.setText("You clicked on Button 2");   

            break;

        case R.id.button3:     

            button3.setText("You clicked on Button 3");

            break;

        case R.id.button4:         

            button4.setText("You clicked on Button 4");

            break;

        case R.id.button5:

            button5.setText("You clicked on Button 5");

            break;

        }

    }

    @Override

    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.activity_main, menu);

 

        return true;

    }

}

 Step 4

Open and update the "AndroidManifest.xml" file from "res/AndroidManifest.xml" and update it as shown below:

<?xml version="1.0" encoding="utf-8"?>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"

    package="com.newandroid.project"

    android:versionCode="1"

    android:versionName="1.0" >

    <uses-sdk

        android:minSdkVersion="8"

        android:targetSdkVersion="17" />

    <application

        android:allowBackup="true"

        android:icon="@drawable/ic_launcher"

        android:label="@string/app_name"

        android:theme="@style/AppTheme" >

        <activity

            android:name="com.newandroid.project.MainActivity"

            android:label="@string/app_name" >

            <intent-filter>

                <action android:name="android.intent.action.MAIN" />

 

                <category android:name="android.intent.category.LAUNCHER" />

            </intent-filter>

        </activity>

    </application>

</manifest>

Step 5

See Output.

First view before calling onClick:

firstview.jpg

After clicking on Button2:

 secondView.jpg

Before clicking on Button3:

thirdview.jpg

After calling the "Quit App" button:

finishmode.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all