Android Button Widget Tutorial With Example

In this post, we will learn about Android Button widget. We will also see different attributes that are used to customise this widget.


Tutorialwing Android Button Tutorial Output

Android Button Tutorial Output

Video Output

Getting Started

Android Button can be defined as below –

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

Different Attributes Of Android Button Widget

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

Attributes in Button are inherited from TextView and View. Some of the popular attributes are –

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

Example Of Android Button Widget

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

Follow the steps below to create new android project. Please ignore the steps if you have already created the project.

1. Creating New Project

Step Description
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 17 as minimum SDK. Then, click next button
4. Then, select Empty Activity => click next => click finish.
5. If you have followed above process correctly, you will get a newly created project successfully. However, you can also visit post to create a new project to know steps in detail.

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

2. Modify Values Folder

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

	<string name="app_name">Button</string>
	<string name="show_message">Show message</string>
	<string name="welcome_message">Welcome to Button Tutorial!</string>

Other values folder have not been changed. So, we are not going to mention it here.

3. Use Button Widget in xml file

Open res/layout/activity_main.xml file. Add below code into it. You may also visit post to use Button widget programmatically in android

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



Here, we have defined android Button widget in xml file. Now, we will access this Button in java file. Then, we will show Toast message on button click.

4. Access Button in java file

Open src/main/java/com.tutorialwing.edittext/ file. Then, add below code into it.

package com.tutorialwing.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 btnShow = findViewById(;
		if (btnShow != null) {
			btnShow.setOnClickListener(new View.OnClickListener() {
				public void onClick(View v) {
					Toast.makeText(MainActivity.this, R.string.welcome_message, Toast.LENGTH_LONG).show();

Since AndroidManifest file is important part of any android application. we are also going to see the content inside this file.

AndroidManifest.xml file

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

<?xml version="1.0" encoding="utf-8"?>
<manifest package="com.tutorialwing.button"

		<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 above.

That’s end of tutorial on Android Button tutorial and it’s example.

Leave a Reply