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 the XML file for every view. And in the Java file, we can use "onClick( View view)" on every view without set "onclickLitener" using get the 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.
  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.     <Button  
  7.         android:id="@+id/button1"  
  8.         android:layout_width="wrap_content"  
  9.         android:layout_height="wrap_content"  
  10.         android:layout_alignParentLeft="true"  
  11.         android:layout_alignParentRight="true"  
  12.         android:layout_alignParentTop="true"  
  13.         android:onClick="onClick"  
  14.         android:text="Button 1" />  
  15.     <Button  
  16.         android:id="@+id/button2"  
  17.         android:layout_width="wrap_content"  
  18.         android:layout_height="wrap_content"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_below="@+id/button1"  
  22.         android:layout_marginTop="36dp"  
  23.         android:onClick="onClick"  
  24.         android:text="Button 2 " />  
  25.     <Button  
  26.         android:id="@+id/button3"  
  27.         android:layout_width="wrap_content"  
  28.         android:layout_height="wrap_content"  
  29.         android:layout_alignParentLeft="true"  
  30.         android:layout_alignParentRight="true"  
  31.         android:layout_below="@+id/button2"  
  32.         android:layout_marginTop="45dp"  
  33.         android:onClick="onClick"  
  34.         android:text="Button 3" />  
  35.     <Button  
  36.         android:id="@+id/button4"  
  37.         android:layout_width="wrap_content"  
  38.         android:layout_height="wrap_content"  
  39.         android:layout_alignParentLeft="true"  
  40.         android:layout_alignParentRight="true"  
  41.         android:layout_below="@+id/button3"  
  42.         android:layout_marginTop="51dp"  
  43.         android:onClick="onClick"  
  44.         android:text="Button 4" />  
  45.     <Button  
  46.         android:id="@+id/button5"  
  47.         android:layout_width="wrap_content"  
  48.         android:layout_height="wrap_content"  
  49.         android:layout_alignParentLeft="true"  
  50.         android:layout_alignParentRight="true"  
  51.         android:layout_below="@+id/button4"  
  52.         android:layout_marginTop="38dp"  
  53.         android:onClick="onClick"  
  54.         android:text="Quit App" />  
  55. </RelativeLayout>  
Step 3
 
Open Java from "src/com.newandroid.project/MainActivity.java" and update it with the following code:
  1. package com.newandroid.project;  
  2. import android.os.Bundle;  
  3. import android.app.Activity;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. public class MainActivity extends Activity {  
  8.       Button button1,button2,button3,button4,button5;  
  9.     @Override  
  10.     protected void onCreate(Bundle savedInstanceState) {  
  11.         super.onCreate(savedInstanceState);  
  12.         setContentView(R.layout.activity_main);  
  13.         button1=(Button) findViewById(R.id.button1);  
  14.         button2=(Button) findViewById(R.id.button2);  
  15.         button3=(Button) findViewById(R.id.button3);  
  16.         button4=(Button) findViewById(R.id.button4);  
  17.         button5=(Button) findViewById(R.id.button5);  
  18.     }  
  19.     public void onClick(View v) {  
  20.         final int id = v.getId();  
  21.         switch (id) {  
  22.         case R.id.button1:  
  23.            button1.setText("You clicked on Button 1");  
  24.             break;  
  25.         case R.id.button2:  
  26.             button2.setText("You clicked on Button 2");  
  27.             break;  
  28.         case R.id.button3:  
  29.             button3.setText("You clicked on Button 3");  
  30.             break;  
  31.         case R.id.button4:  
  32.             button4.setText("You clicked on Button 4");  
  33.             break;  
  34.         case R.id.button5:  
  35.             button5.setText("You clicked on Button 5");  
  36.             break;  
  37.         }  
  38.     }  
  39.     @Override  
  40.     public boolean onCreateOptionsMenu(Menu menu) {  
  41.         // Inflate the menu; this adds items to the action bar if it is present.  
  42.         getMenuInflater().inflate(R.menu.activity_main, menu);  
  43.         return true;  
  44.     }  
  45. }  
Step 4
 
Open and update the "AndroidManifest.xml" file from "res/AndroidManifest.xml" and update it as shown below:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.newandroid.project"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="8"  
  8.         android:targetSdkVersion="17" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name"  
  13.         android:theme="@style/AppTheme" >  
  14.         <activity  
  15.             android:name="com.newandroid.project.MainActivity"  
  16.             android:label="@string/app_name" >  
  17.             <intent-filter>  
  18.                 <action android:name="android.intent.action.MAIN" />  
  19.                 <category android:name="android.intent.category.LAUNCHER" />  
  20.             </intent-filter>  
  21.         </activity>  
  22.     </application>  
  23. </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


Similar Articles