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); </li> <li>Set the OnClickListener for the Button 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. ![]()
Or, you can just download the apk here.![]()
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-
- Complete Steps of installing Android SDK and Eclipse Indigo.
- Writing 'HelloWorld' application in android.
- Installing .apk file on android emulator.
Pingback: Android : Using CheckBox with Example « Suvendu's Blog
Pingback: Blue Ray Plus - Latest Technology News
Hi Suvendu, thats great and detailed write up, sure it helped me.
But what if there are multiple buttons and multiple onclicks?
Hi I have write this lecture and may be this will i help you .. I really need your appreciation thanks
http://themasterworld.com/response-on-click-events-in-android-studio/
Fabulous tips thanks..
if you want to know more about android-basic-tutorial-get-current-date then goto this link.
http://exandroidstudio.blogspot.in/2015/02/android-basic-tutorial-get-current-date.html
Can We Set Multiple Edittext With Only One Button To Show And Hide It ??
this is the best site for learning
thanks for this example and please tell me how to save this date into the databse?????
Thank you.