Android Button Using Kotlin

android button kotlin example
Android Button

Read later
Save important notes and articles from internet using arusaquotes app.

Create Project

Open Android Studio and create a new project.

XML Code

Go to app > resource > layout > activity_main.xml. Add two buttons an give an unique id for both.

android:id="@+id/firstBtn"
android:id="@+id/secondBtn"

Set background color of Button

android:backgroundTint="@color/teal_200"

Set padding in Button

android:padding="12dp"

Enable/Disable button

android:enabled="false"

Lowercase/uppercase of button text

android:textAllCaps="false"

Set text color of button

android:textColor="@color/black"

Setting icon in android button

app:icon="@drawable/ic_android_black_24dp"
        app:iconPadding="8dp"

Here is the final xml code activity_main.xml.

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.LinearLayoutCompat 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"
    android:gravity="center"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/firstBtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="20dp"
        android:backgroundTint="@color/teal_200"
        android:padding="12dp"
        android:text="NotesJam"
        android:textAllCaps="false"
        android:textColor="@color/black"
        android:textSize="28sp" />

    <Button
        android:id="@+id/secondBtn"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:enabled="false"
        android:text="NotesJam"
        android:textSize="22sp"
        app:icon="@drawable/ic_android_black_24dp"
        app:iconPadding="8dp" />

</androidx.appcompat.widget.LinearLayoutCompat>

Kotlin Code

Go to app > java > com.example.buttondemo (Maybe different in your case) > MainActivity.kt and put this code outside of onCreate function.

private lateinit var firstBtn: Button
    private lateinit var secondBtn: Button

Put this code after setContentView()

firstBtn = findViewById(R.id.firstBtn)
        secondBtn = findViewById(R.id.secondBtn)

Setting click event on button

//android kotlin button onclick listener
        firstBtn.setOnClickListener {
            //We are enabling the second button
            secondBtn.isEnabled = true
        }

Setting visiblity of button

//android kotlin button visibility
        firstBtn.visibility = View.GONE
        firstBtn.visibility = View.VISIBLE

Here is the final Kotlin code MainActivity.kt

package com.example.buttondemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button

class MainActivity : AppCompatActivity() {
    private lateinit var firstBtn: Button
    private lateinit var secondBtn: Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        firstBtn = findViewById(R.id.firstBtn)
        secondBtn = findViewById(R.id.secondBtn)

        //android kotlin button onclick listener
        firstBtn.setOnClickListener {
            //We are enabling the second button
            secondBtn.isEnabled = true
        }

        //android kotlin button visibility
        firstBtn.visibility = View.GONE
        firstBtn.visibility = View.VISIBLE

    }
}

Output

Here is the final output.

android button kotlin
Android Button

Learn Android App Development using Kotlin.

Start Learning