Button in Kotlin

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:

  1. Click on File, then New => New Project.
  2. After that include the Kotlin support and click on next.
  3. Select the minimum SDK as per convenience and click next button.
  4. 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

filter_none

edit
close

play_arrow

link
brightness_4
code

<resources>
    <string name="app_name">ButtonInKotlin</string>
    <string name="btn">Button</string>
    <string name="message">Hello Geeks! This is a Button.</string>
</resources>

chevron_right


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.



filter_none

edit
close

play_arrow

link
brightness_4
code

<?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>

chevron_right


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

filter_none

edit
close

play_arrow

link
brightness_4
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() }
    }
}

chevron_right


AndroidManifest.xml file

Go to main/AndroidManifest.xml file and add the following code

filter_none

edit
close

play_arrow

link
brightness_4
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>

chevron_right


Run as Emulator:

If we execute this application we will get the following output

 



My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

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.




Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.