android kotlin - Material AlertDialog example

MainActivity.kt

package com.cfsuman.kotlintutorials

import android.app.Activity
import android.app.AlertDialog
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.widget.*
import androidx.core.content.ContextCompat
import com.google.android.material.dialog.MaterialAlertDialogBuilder


class MainActivity : Activity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val context:MainActivity = this

        // get the widgets reference from XML layout
        val button = findViewById<Button>(R.id.button)


        button.setOnClickListener {
            val builder = MaterialAlertDialogBuilder(context)

            // dialog title
            builder.setTitle("Dialog Title")

            // drawable for dialog title
            builder.setIcon(R.drawable.ic_action_help_outline)

            // dialog message
            builder.setMessage("Are you want to download the file?.")

            // dialog background color
            builder.background = ColorDrawable(
                Color.parseColor("#FEFEFA")
            )

            // alert dialog positive button
            builder.setPositiveButton("Download"){ dialog,which->
                // do something on positive button click
            }

            // icon for positive button
            val drawable = ContextCompat.getDrawable(
                context,R.drawable.ic_action_arrow_download)
            builder.setPositiveButtonIcon(
                drawable
            )

            builder.setNegativeButton("No"){dialog,which->
                // do something when negative button clicked
            }

            builder.setNeutralButton("Cancel"){dialog,which->
                // do something on neutral button click
            }


            builder.setOnCancelListener {
                // do something on cancel listener
            }

            builder.setOnDismissListener {
                // do something on dismiss listener
            }

            // set dialog non cancelable
            builder.setCancelable(false)


            // finally, create the alert dialog and show it
            val dialog = builder.create()


            // resize positive button icon
            dialog.setOnShowListener {
                val positiveButton = dialog
                    .getButton(AlertDialog.BUTTON_POSITIVE)
                val textSize = positiveButton.textSize.toInt() + 5
                val positiveDrawableResized = drawable?.apply {
                    setBounds(0, 0, textSize, textSize) }
                positiveButton.setCompoundDrawables(
                    positiveDrawableResized, null, null, null)
            }

            // Show the alert dialog
            dialog.show()
        }
    }
}
activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#DCDCDC"
    android:padding="24dp">

    <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Dialog"
        android:textAllCaps="false"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]

// Material components
implementation 'com.google.android.material:material:1.6.1'
More android examples