Android Button Using Kotlin With Example

Hello Readers! In this post, we will learn about Android Button using kotlin. We will also go through different attributes that are used commonly.


Tutorialwing Android Button Using Kotlin Output Use Button Widget using Kotlin

Tutorialwing Android Button Using Kotlin Output

Video Output

Android Button can be defined as below –

Android Button is an user interface that are used to perform some action when clicked or tapped.

Different attributes of Android Button Widget

Different attributes of Android Button Widget are listed below. However, If you want to know more attributes you can visit android official documentation on Button Widget.

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

Sr. XML Attributes Description
1 android:background This attribute is used to set background to this View.
2 android:backgroundTint This attribute is used to set tint to the background.
3 android:clickable This attribute is used to set 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 using kotlin

In this section, we will learn how to use Android Button using kotlin in any android application. At first, we will create an android application. Then, we will use Android Button using kotlin.

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

1. Creating New Project in Kotlin

Step Description
1. Open Android Studio.
2. Go to File => New => New Project. Write application name as Button. Then, check Include Kotlin Support and 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.

You can also visit post to get detailed steps to create new project in android. The topics are covered in java. However, the steps are similar in kotllin.

Now, we will modify xml and kotlin file to use Button in the application.

2. Modify Values Folder

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

Other values folders 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 can also our post to use button programmatically in kotlin

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



Here, we have defined Button in xml file. Now, we will access this Button in kotlin file. Then, we will show a Toast message when this Button is clicked.

4. Access Button in Kotlin file

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

package com.tutorialwing.button

import android.os.Bundle
import android.widget.Button
import android.widget.Toast

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {

        val btnShow = findViewById<Button>(
        btnShow?.setOnClickListener { Toast.makeText(this@MainActivity, R.string.welcome_message, Toast.LENGTH_LONG).show() }

Since AndroidManifest file is very important part of any android application, we are also going to mention it here.

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"/>


When we run the application, we will get output as shown above.

That’s end of tutorial on Android Button using Kotlin.