Android : Using Button & Click-Event with example

Introduction :

In this article we will learn, how to use Button and Listen for Click-Events. We shall take an example to understand all these . To make the example clear and easy to understand we shall use another control called EditText.

Implementation using  Example :

  • Create a new android project with name ButtonTutorial.
    [If you are a beginner and don't know how to create a project then read it here Creating HelloWorld Application in Android]
  • Type you application name as My Button Tutorial.
  • Provide package name as com.suvendu.tutorials.button
  • Open main.xml and delete the single TextView already present in the page(may be with a text ‘Hello World, ButtonTutorialActivity!’.
  • Drag a EditText and a Button on to the layout.
  • Change the id of the EditText to ‘@+id/txtShowCurTime
  • Change the id of the Button to ‘@+id/btnGenCurTime
  • Add a new string to strings.xml with Name ‘button’ and Value Generate Current Time
  • Change the text of the Button to ‘@string/button’
    [Your layout is ready now and the main.xml should look like this]

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent"
     android:orientation="vertical" >
    
    <EditText
     android:id="@+id/txtShowCurTime"
     android:layout_width="match_parent"
     android:layout_height="wrap_content" >
     <requestFocus />
     </EditText>
    
     <Button
     android:id="@+id/btnGenCurTime"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:text="@string/button" />
    
    </LinearLayout>
    
    
  • Open your default activity file (Ex: ButtonTutorialActivity.java)
  • Declare two global variables
    Button btnCTime;
     EditText txtCTime;
    
    
  •  import respective packages
    import android.widget.Button;
    import android.widget.EditText;
    
    
  •  Link these variables to the layout in OnCreate() as follows
    btnCTime=(Button)findViewById(R.id.btnGenCurTime);
     txtCTime=(EditText)findViewById(R.id.txtShowCurTime);
    
    
  • We don’t want the OnScreenKeyBoard to appear when the cursor is on EditText so, let disable this.
    txtCTime.setInputType(InputType.TYPE_NULL);
    1</li>
    	<li>Set the OnClickListener for the Button
    1
    btnCTime.setOnClickListener(new OnClickListener() {
    
     @Override
     public void onClick(View vw) {
    
     }
     });
    
    
  • import the package for OnClickListener
    import android.view.View.OnClickListener;
    
  • Now do the logic inside onClick() to show the current time
    txtCTime.setText(new Date().toString());
    
  • You need to import java package for using Date
    import java.util.Date;
    
  • Finally, the default activity file should look like
    package com.suvendu.tutorials.button;
    
    import java.util.Date;
    
    import android.app.Activity;
    import android.os.Bundle;
    import android.text.InputType;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;
    import android.widget.EditText;
    
    public class ButtonTutorialActivity extends Activity {
        Button btnCTime;
        EditText txtCTime;
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
            btnCTime=(Button)findViewById(R.id.btnGenCurTime);
            txtCTime=(EditText)findViewById(R.id.txtShowCurTime);
            txtCTime.setInputType(InputType.TYPE_NULL);
            btnCTime.setOnClickListener(new OnClickListener() {
                @Override
                public void onClick(View vw) {
                    txtCTime.setText(new Date().toString());
                }
            });
        }
    }
    

Downloads :

You can download the full project here. Download

Or, you can just download the apk here.Download

Conclusion :

We are able to use a Button control in Android and can use its OnClick event to do various activities.
I hope  you enjoyed this tutorial.

Please give your valuable feedback and stay tuned for more ..

You may also like-

Simple example of Intent in Android 

If you are a beginner then you may also like following articles-

About these ads

3 thoughts on “Android : Using Button & Click-Event with example

  1. Pingback: Android : Using CheckBox with Example « Suvendu's Blog

  2. Pingback: Blue Ray Plus - Latest Technology News

  3. Hi Suvendu, thats great and detailed write up, sure it helped me.

    But what if there are multiple buttons and multiple onclicks?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s