Android Button

In this tutorial, we will learn about Android Button widget. We will also see various attributes used to customise this widget.

Android Button can be defined as below –

"Android Button is user interface that user can tap or click to perform some action."

Different attributes that are used in button are list below. However, if you want to see the complete list of attributes, however you need to visit Android's official button widget documentation. Here, we are listing commonly used attributes.

TextView and View inherit the attributes in button. Some of the popular attributes are –



S. No. XML Attributes Description
1 android:id This is unique id of the Button to uniquely identify the Button.
2 android:height Sets height of the Button.
3 android:width Sets width of the Button.
4 android:text Sets the text of the Button.
5 android:textAllCaps Shows text in capital letters.
6 android:textColor Sets color of the text.
7 android:textSize Sets size of the text.
8 android:textStyle Sets style of the text. For example, bold, italic etc.
9 android:typeface Sets typeface of the text. For example, normal, sans, serif, monospace.
10 android:clickable Sets true when you want to make this View clickable. Otherwise, set false.
11 android:background Sets background to this View.
12 android:backgroundTint Sets tint to the background.
13 android:drawableBottom This is drawable to be drawn at bottom of the text.
14 android:drawableEnd This is drawable to be drawn to end of the text.
15 android:drawableLeft This is drawable to be drawn to left of the text.
16 android:drawablePadding This is padding of the drawable.
17 android:drawableRight This is drawable to be drawn to right of the text.
18 android:drawableStart This is drawable to be drawn to start of the text.
19 android:drawableTop This is drawable to be drawn at top of the text.

In next, we will learn how to use android Button widget in any android application. At first, we will create android application. Then we'll use the Button widget in android application.

To create new android project, follow the steps below. If you have already created the project. Please ignore the steps.

S. No. Steps
1 Open Android Studio.
2 Go to File => New => New Project. Write application name as Button. Then, click next button.
3 Select minimum SDK you need. However, we have selected 14 as minimum SDK. Then, click next button.
4 Then, select Empty Activity => click next => click finish.



If you have correctly followed the above process, you will succeed in getting a newly create project. However, you can also visit tutorial to Create a New Project to know steps in detail.

Since we have a project now. To use the Button widget, we will modify the xml and java file. Please follow the steps below.

Open res/values/strings.xml file. Then, add below code into it.

<string name="app_name">Button</string> 
<string name="click_me">Click Me!</string> 
<string name="welcome_message">Welcome to Button Tutorial!</string> 

Other values folder have not been changed. So, here we won't mention it .

Open res/layout/activity_main.xml file. Add below code into it.

<?xml version="1.0" encoding="utf-8"?>
< xmlns:android=""

app:layout_constraintTop_toTopOf="parent" />




Here, in xml file, we have set the Android button widget. Now, in java file, we're going to access this button. Then, clicking on the button we'll show the toast message.

Open src/main/java/com.ukacademe.button/MainActivity.javafile. Then, add below code into it.

package com.ukacademe.button;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    protected void onCreate(Bundle savedInstanceState) {

        Button btnClickMe = (Button)findViewById(;
        btnClickMe.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Toast.makeText(MainActivity.this,"Welcome to Button Tutorial!", Toast.LENGTH_LONG).show();

Since the file of AndroidManifest is an important part of any android application, we will also see the content in this file.

Code inside main/AndroidManifest.xml file is as below.

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android=""

<activity android:name=".MainActivity">
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />


Now, when we run the application, we will get output as shown below.

UK Academe-Button-Output