In Android a Button usually represent a Push Button. Button are used to perform a specific action when user pressed or clicked on it.Meaning that a button is user interface element the user can tap or click to perform an action. There are different types of the button including CompoundButton,ToggleButton,CheckBox,Switch and a RadioButton.
In Android API, Button is a subclass of the TextView and CompoundButton is subclass of the Button Class.
public class Button extends TextView
In User Interface a button may consists of text or an icon (or both text and an icon) that communicates what action occurs when the user touches it.
A button can be created in UI using XML code in Layout (XML) file or programatically in JAVA or using drag and drop facility of Android.
Creating a Button using XML Code.
<Button android:id="@+id/simpleButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Android Button"/>
Above XML Code create a simple button with text "Android Button"
As we have already stated that a Button may contains only text, only icon or both. A button which contains icon or both an icon and a text is called "imageButton"
<ImageButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/button_icon" ... />
Above XML Code can be used to create a Button with only icon and output will be look like this
We can also create a Button have both an icon and an image like
<Button android:id="@+id/simpleButton" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_centerInParent="true" android:background="#147D03" android:text="Download Code" android:textSize="20sp" android:padding="15dp" android:textStyle="bold|italic" android:drawableBottom="@drawable/ic_launcher"/><!--image drawable on button-->
and the output will be look like
Creating a button programmatically in Android
A button can be created programatically in android using JAVA. for Example following code creates a button and adds to LinearLayout filepackage com.android_examples.com.dynamicbuttonandroid; import android.app.Activity; import android.os.Bundle; import android.view.Gravity; import android.widget.Button; import android.widget.LinearLayout; import android.widget.LinearLayout.LayoutParams; public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); LinearLayout linearLayout = new LinearLayout(this); Button button = new Button(this); button.setText(" Button Programmatically "); button.setTextSize(20); button.setGravity(Gravity.CENTER); linearLayout.addView(button); this.setContentView(linearLayout, new LinearLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); } }
and main Layout(XML) file will be look like this
<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" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="com.android_examples.com.dynamicbuttonandroid.MainActivity" > </RelativeLayout>
And the output will look like this
A button has number of attributes and most important include
android:id ( this is used to uniquely identity and object in the application)
android:text
android:src (for imageButton only)
android:textStyle
android:backgroud
android:drawabletop ( also for bottom,left and right)
Responding to the Button Events (click event)
When the user clicks a button, the
Button
object receives an on-click event.To define the click event handler for a button, add the
android:onClick
attribute to the <Button>
element in your XML layout. The value for this attribute must be the name of the method you want to call in response to a click event. The Activity
hosting the layout must then implement the corresponding method.For example, here's a layout with a button using
android:onClick
:<?xml version="1.0" encoding="utf-8"?> <Button xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/button_send" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/button_send" android:onClick="sendMessage" />
Within the
Activity
that hosts this layout, the following method handles the click event:/** Called when the user touches the button */ public void sendMessage(View view) { // Do something in response to button click }
The method you declare in the
android:onClick
attribute must have a signature exactly as shown above. Specifically, the method must:- Be public
- Return void
- Define a
View
as its only parameter (this will be theView
that was clicked)
and Alternatively we can do the same using using an OnClickListener
You can also declare the click event handler programmatically rather than in an XML layout. This might be necessary if you instantiate the
Button
at runtime or you need to declare the click behavior in a Fragment
subclass.To declare the event handler programmatically, create an
View.OnClickListener
object and assign it to the button by calling setOnClickListener(View.OnClickListener)
. For example:Button button = (Button) findViewById(R.id.button_send); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Do something in response to button click } });