A Button is an user interface that are used to perform some action when clicked or tapped. So let us learn how to create a button in android studio using Kotlin.
First we create a new project by following the below steps:
- Click on File, then New => New Project.
- After that include the Kotlin support and click on next.
- Select the minimum SDK as per convenience and click next button.
- Then select the Empty activity => next => finish.
Different attributes for Button widget
| XML attributes | Description |
|---|---|
| android:id | Used to specify the id of the view. |
| android:text | Used to the display text of button. |
| android:textColor | Used to the display color of the text. |
| android:textSize | Used to the display size of the text. |
| android:textStyle | Used to the display style of the text like Bold, Italic etc. |
| android:textAllCaps | Used to the display text in the Capital letters. |
| android:background | Used to set the background of the view. |
| android:padding | Used to set the padding of the view. |
| android:visibilty | Used to set the visibility of the view. |
| android:gravity | Used to specify the gravity of the view like center, top, bottom etc |
Go to the res/values folder and add the following code
<resources> <string name="app_name">ButtonInKotlin</string> <string name="btn">Button</string> <string name="message">Hello Geeks! This is a Button.</string> </resources> |
Adding the button widget in the xml file
Go to res/layout/activity_main.xml file and add the following code. This code creates a button in our app.
<?xml version="1.0" encoding="utf-8"?> <LinearLayout android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center" android:orientation="vertical"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/btn"> </Button> </LinearLayout> |
Accessing the button in MainActivity.kt file
Go to src/main/java/your_package_name_will_appear_here/MainActivity.kt and add the following code
package com.geeksforgeeks.myfirstkotlinapp import androidx.appcompat.app.AppCompatActivity import android.os.Bundle 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 button = findViewById<Button>(R.id.button) button?.setOnClickListener() { Toast.makeText(this@MainActivity, R.string.message, Toast.LENGTH_LONG).show() } } } |
AndroidManifest.xml file
Go to main/AndroidManifest.xml file and add the following code
<?xml version="1.0" encoding="utf-8"?> package="com.geeksforgeeks.myfirstkotlinapp"> <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> |
Run as Emulator:
If we execute this application we will get the following output
Recommended Posts:
- Dynamic Button in Kotlin
- button tag vs input type="button" attribute
- How to trigger HTML button after hitting enter button in textbox using JavaScript ?
- How to create a radio button similar to toggle button using Bootstrap ?
- Kotlin Data Types
- Hello World program in Kotlin
- Kotlin | Retrieve Collection Parts
- Destructuring Declarations in Kotlin
- DatePicker in Kotlin
- Kotlin labeled continue
- Introduction to Kotlin
- Kotlin Type Conversion
- Kotlin Exception Handling | try, catch, throw and finally
- Kotlin if-else expression
- Kotlin Environment setup for Command Line
- Kotlin constructor
- Kotlin Environment setup with Intellij IDEA
- Kotlin Nested class and Inner class
- Kotlin Variables
- Kotlin Operators
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

