Home » Android Button Example – Kotlin

Android Button Example – Kotlin

android kotlin button example

In this post, we will see the android button example in Kotlin. The Android button is the most commonly used UI element in Android. Specific action can be performed when the user clicks on the button.

Let us see an Android button example, In which I have defined a button labeled Click Me. If the user clicks this button, a Toast message “Button Clicked” will be displayed.

To define a button in an XML layout file we used <Button> tag. The Android Button Widget has different attributes using which you can configure your button in the XML layout file.

Kotlin Android Button Example

I have created a new Android project with a minimum SDK version as 23 and with Empty Activity. I have defined a button in activity_main.xml file and Kotlin code to handle the button click event is written in MainActivity.kt.

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/buttonClickMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt

package com.example.buttonexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        buttonClickMe.setOnClickListener{
            Toast.makeText(this, "Button Clicked", Toast.LENGTH_LONG).show()
        }
    }
}

The output of the above example:

android button onclicklistener

In the above example, I have used OnClickListener to handle the button click event. We can also do the same using android:onClick attribute.

To handle the click event using android:onClick, we want to assign a method to the button which we have defined in our XML file.

android:onClick=”showMessage”

The method named showMessage is defined in MainActivity.kt to handle click event and it should always be a public method and should take View as a parameter. Here is an example of using android:onClick.

activity_main.xml

<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
    <Button
        android:id="@+id/buttonClickMe"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Click Me"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        android:onClick="showMessage"/>
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.kt

package com.example.buttonexample
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
    fun showMessage(view: View) {
        Toast.makeText(this,"Button Clicked!!!", Toast.LENGTH_LONG).show()
    }
}

Also Check:

Leave a Reply

Your email address will not be published.