Greetings!
We have recently published 100+ articles on android tutorials with kotlin and java. If you need, you may visit Android Tutorial for beginners page. You can also check Kotlin Tutorial for beginners. Also, if you are interested in content writing, you can mail us at tutorialwing@gmail.com.Hello Readers! In this post, we will learn about Android Button using kotlin. We will also go through different attributes that are used commonly.
Output

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
<resources> <string name="app_name">ButtonUsingKotlin</string> <string name="show_message">Show message</string> <string name="welcome_message">Welcome to Button Tutorial Using Kotlin!</string> </resources>
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"?> <LinearLayout android:id="@+id/activity_main" xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/btnShow" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/show_message"> </Button> </LinearLayout>
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.support.v7.app.AppCompatActivity import android.widget.Button import android.widget.Toast class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) val btnShow = findViewById<Button>(R.id.btnShow) 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" xmlns:android="http://schemas.android.com/apk/res/android"> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN"/> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
When we run the application, we will get output as shown above.
That’s end of tutorial on Android Button using Kotlin.