Work With Button Click In Android

Introduction

 
In this article, I describe how to work a button in Android when it is clicked or pressed. In this article, I develop an action that can be performed by a button, as you will see in the output of my application.
 
Here, first of all, we must create a new Android application project in Android. Then we will use the instructions provided here.
 
Step 1
 
In this step create a new application as shown below using "File" -> "New" -> "Android Application Project".
 
ButtonAction.jpg
 
Step 2
 
In this step we will open "activity_main.xml" and update it as in the following:
  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_alignParentTop="true"  
  11.         android:layout_centerHorizontal="true"  
  12.         android:layout_marginTop="16dp"  
  13.         android:text="@string/click_btn" />  
  14.     <TextView  
  15.         android:id="@+id/textView1"  
  16.         android:layout_width="wrap_content"  
  17.         android:layout_height="wrap_content"  
  18.         android:layout_alignParentBottom="true"  
  19.         android:layout_alignParentLeft="true"  
  20.         android:layout_alignParentRight="true"  
  21.         android:layout_below="@+id/button1"  
  22.         android:text="@string/txt_view"  
  23.         android:textAppearance="?android:attr/textAppearanceMedium" />  
  24. </RelativeLayout> 
Step 3
 
In this step I will open "string.xml" and update it as in the following:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <resources>  
  3.     <string name="app_name">ButtonAction</string>  
  4.     <string name="hello_world">Hello world!</string>  
  5.     <string name="menu_settings">Settings</string>  
  6.     <string name="click_btn">Click Here</string>  
  7.     <string name="txt_view"></string>  
  8. </resources> 
Step 4
 
In this step, I will open my "MainActivity.java"  file from my project and I will update it with my logic as in the following:
  1. package com.example.buttonaction;  
  2. import android.app.Activity;  
  3. import android.os.Bundle;  
  4. import android.view.Menu;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9. public class MainActivity extends Activity {  
  10.       int flage=1;  
  11.       Button btn;  
  12.       TextView tv;  
  13.       int i=1;  
  14.       @Override  
  15.       protected void onCreate(Bundle savedInstanceState) {  
  16.             super.onCreate(savedInstanceState);  
  17.             setContentView(R.layout.activity_main);  
  18.             btn=(Button) findViewById(R.id.button1);  
  19.             tv=(TextView) findViewById(R.id.textView1);  
  20.             btn.setOnClickListener(new OnClickListener() {  
  21.                   @Override  
  22.                   public void onClick(View v) {  
  23.                         // TODO Auto-generated method stub  
  24.                         if(flage==1)  
  25.                         {                        
  26.                               switch(i)  
  27.                               {  
  28.                               case 1:  
  29.                                     tv.setText(i+" You Clicked Successfully ");  
  30.                                     break;  
  31.                               case 2:  
  32.                                     tv.setText(i+" You Clicked Successfully ");  
  33.                                     break;  
  34.                               case 3:  
  35.                                     tv.setText(i+" You Clicked Successfully ");  
  36.                                     break;  
  37.                               case 4:  
  38.                                     tv.setText(i+" You Clicked Successfully ");  
  39.                                     break;  
  40.                               case 5:  
  41.                                     tv.setText(i+" You Clicked Successfully ");  
  42.                                     break;  
  43.                               case 6:  
  44.                                     tv.setText(i+" You Clicked Successfully,\n Now you are going to finish this app. ");  
  45.                                     break;  
  46.                                     default:  
  47.                               }  
  48.                          
  49.                         flage=0;  
  50.                         i++;  
  51.                         }  
  52.                         else if(flage==0)  
  53.                         {  
  54.                               tv.setText(" ");  
  55.                               if(i>6)  
  56.                               {  
  57.                                     finish();  
  58.                               }  
  59.                               flage=1;  
  60.                         }  
  61.                   }  
  62.             });                
  63.       }  
  64.    
  65.       @Override  
  66.       public boolean onCreateOptionsMenu(Menu menu) {  
  67.             // Inflate the menu; this adds items to the action bar if it is present.  
  68.             getMenuInflater().inflate(R.menu.activity_main, menu);  
  69.             return true;  
  70.       }  
Step 5
 
And if we need an update in "Manifest.xml" I will also update it as in the following code.
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.buttonaction"  
  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.example.buttonaction.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.    
  24. </manifest> 
Output
 
In the output, you will see on the first 5-6 clicks that this button will work and after 6 clicks it will self-destroy because here I use the finish() method.
You can see the Output below.
 
Output on the first click:
 
ButtonActionOuput1.jpg
 
Output on 5th click:
 
ButtonActionOuput2.jpg
 
Output on 6th click:
 
ButtonActionOuput3.jpg
 
Output after 6th click and before Auto Destroy:
 
ButtonActionOuput4.jpg


Similar Articles