Android Button Control - Android Button Example | W3school

A Button is a normal push button which is also one of the often used UI control. To use a button at the run time of an application, the user needs to either press it or click it to perform a specific action linked to it.

Also Read: Android AutoCompleteTextView Control

In the above image, you can easily see dial button, cancel button, and OK button. The two of them are encircled so that you can easily identify them.

Various Attributes of Button Control
We have compiled a list of important attributes related to the button control. For the complete list of Button control attributes, you can visit the official Android document. Well, other than attributes, you will also be introduced to the major associated methods. With the help of these methods, you will be able to change the value of all the attributes at the run time.


The following list of Button control attributes is inherited from the android.widget.TextView class.

S.No. Attribute Description
1 android:text * This attribute accepts the text to be displayed on the screen.
2 android:autoText * This attribute lets the user enter text in the provided field.
* It has the ability to auto-correct a few of the common spelling mistakes.
3 android:editable * This attribute depicts that the provided field can accept the input value from the user.
4 android:drawableRight * This attribute accepts drawable as its value which is then be displayed to the right of the text.
5 android:drawableBottom * This attribute accepts drawable as its value which is then be displayed below the text.

The following list of Button control attributes is inherited from the android.view.View class.

S.No. Attribute Description
1 android:id * This attribute accepts the identifier (id) name for the associated view.
2 android:onClick * This is a method which gets invoked whenever the view is clicked.
3 android:visibility * If set, this attribute decides the initial visibility of a view.
4 android:background * This attribute accepts a drawable value which will later be set as the view’s background.
5 android:contentDescription * The value of this attribute is present in the text format which will brief about the content of the view.

To understand the use of above-mentioned attributes, let’s take a look at the following example. Here, you will get to know about the step-to-step procedure for the creation of an Android application by the use of Linear layout and Button control.

Step 1: Start by opening the Android Studio IDE. Create an Android application and save it as myapplication. Keep this app under the com.example.w3school.myapplication package. To understand this step more clearly, take a look at the already explained Hello World Example chapter.

Step 2: Add a click event to the main activity file, which is To find this file, look into the src/ directory. The existing content of the file will be containing the fundamental lifecycle callback methods. 

package com.example.w3school.myapplication;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast; 

public class MainActivity extends ActionBarActivity
   Button b1,b2,b3;   

   protected void onCreate(Bundle savedInstanceState)
      b1.setOnClickListener(new View.OnClickListener()
         public void onClick(View v)
            Toast.makeText(MainActivity.this,”YOUR MESSAGE”,Toast.LENGTH_LONG).show();


Step 3: Update the activity_main.xml file with the content provided below. You can easily locate this file by tracing the res/layout/ path. Well, in this file, you will be including the required button control. 

<?xml version=”1.0″ encoding=”utf-8″?>

      android:text=”Button Control”
      android:textSize=”30dp” />      

      android:layout_centerHorizontal=”true” />      

      android:layout_centerHorizontal=”true” />      

      android:layout_alignEnd=”@+id/imageButton” />      

      android:layout_alignEnd=”@+id/editText” />      

Step 4: In strings.xml file, you need to define the new string constants to be used in your application. You can find this file in the res/values/ directory.

<?xml version=”1.0″ encoding=”utf-8″?>
   <string name=”app_name”>myapplication</string>


Step 5: Make sure that you leave the AndroidManifest.xml file as it is. The default content of the file will look like the following.

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=””
   package=”com.example.saira_000.myapplication” >      

      android:theme=”@style/AppTheme” >      

         android:label=”@string/app_name” >      
            <action android:name=”android.intent.action.MAIN” />
            <category android:name=”android.intent.category.LAUNCHER” />

Step 6: Now, you just need to run your myapplication on the Android Studio IDE. We are assuming that you have already created your AVD during the environment setup. Well, to run this application, just identify the Run button from the toolbar and click on it. The Studio will initiate the process by installing your application on the AVD. If everything falls in the right place then you will be displayed with the final output on the Emulator window.