Android kotlin - AlertDialog example

Last updated Jan 01, 2020

Hello Guys, today we are going to learn how to display AlertDialog in Android with Kotlin.

Step 1: Create a Project

 

Step 2: Create UI with Button to show the Dialog

 

 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        xmlns:tools="https://schemas.android.com/tools"
        xmlns:app="https://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".ExampleActivity">
    <androidx.appcompat.widget.Toolbar
            android:layout_width="match_parent"
           app:title="Kotlin - Display Dialog "
            android:background="#F00857"
            app:titleTextColor="#FFF"
            android:layout_height="wrap_content">

    </androidx.appcompat.widget.Toolbar>
    <androidx.appcompat.widget.AppCompatButton
            android:id="@+id/btn_showDialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Show Me"
            android:textColor="#FFF"
            android:textSize="20sp"
            android:gravity="center"
            android:layout_marginTop="50dp"
            android:background="#F00857"
            android:layout_gravity="center"
            android:minHeight="30dp"
            android:minEms="6"


    />

</LinearLayout>

 

 

Step 3: Create Alert Dialog layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android" android:orientation="vertical"
              android:background="#ffffff" android:layout_width="match_parent" android:layout_height="wrap_content"
              android:weightSum="5">
    <LinearLayout android:orientation="vertical" android:background="#ffffff" android:layout_width="match_parent"
                  android:layout_height="50dp" android:layout_weight="2">
        <LinearLayout android:orientation="horizontal" android:layout_width="match_parent" android:layout_height="70dp"
                      android:layout_weight="1">
            <ImageView android:textColor="#000000"
                       android:layout_gravity="left|center_vertical|center_horizontal|center"
                       android:src="@drawable/solution" android:background="@color/transparent"
                       android:layout_width="40dp" android:layout_height="36dp"
                       android:layout_marginLeft="10dp" android:layout_marginTop="0dp"
                       android:layout_marginRight="0dp"/>
            <TextView android:textSize="20dp" android:textColor="#000000"
                                                         android:layout_gravity="center_vertical"
                                                         android:id="@+id/txtGotoTitle" android:paddingLeft="10dp"
                                                         android:layout_width="wrap_content"
                                                         android:layout_height="wrap_content" android:text="Solution"/>
        </LinearLayout>
    </LinearLayout>
    <View android:background="#e36b23" android:layout_width="match_parent" android:layout_height="6px"/>
    <androidx.core.widget.NestedScrollView android:layout_width="match_parent"
                                           android:layout_height="wrap_content">
        <TextView android:textSize="14dp" android:textColor="#000000"
                                                     android:layout_gravity="left"
                                                     android:id="@+id/txtSolution" android:padding="5dp"
                                                     android:paddingLeft="15dp"
                                                     android:layout_width="wrap_content"
                                                     android:layout_height="wrap_content" android:text=""/>

    </androidx.core.widget.NestedScrollView>
    <com.rrtutors.mathquize.views.CustomButton android:textSize="15dp" android:textColor="#ffffff"
                                               android:layout_gravity="bottom"
                                               android:id="@+id/btnGotIt" android:background="#e36b23"
                                               android:layout_width="match_parent"
                                               android:layout_height="match_parent" android:layout_marginTop="0dp"
                                               android:layout_marginRight="0dp" android:text="Got it!"/>
</LinearLayout>

 

Step 4: Handle Kotlin class to show the Dialog

package com.rrtutors.mathquize

import android.app.Dialog
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.View
import android.widget.Button
import android.widget.TextView
import com.rrtutors.mathquize.aptitude.QuizeActivity
import kotlinx.android.synthetic.main.activity_example.*
import kotlinx.android.synthetic.main.activity_quize.*

class ExampleActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_example)
        btn_showDialog.setOnClickListener({
            val dialog = Dialog(this)
            dialog.requestWindowFeature(1)
            dialog.setContentView(R.layout.aptitude_que_solution)

            val txtSolution = dialog.findViewById(R.id.txtSolution) as TextView

            txtSolution.setText("You Got 90% in your Exam\n You selected for Next Level")
            val btnGotIt = dialog.findViewById(R.id.btnGotIt) as Button

            btnGotIt.setOnClickListener(object : View.OnClickListener {
                override fun onClick(v: View?) {
                    dialog.dismiss()
                }

            })
            dialog.show()
        })
    }
}

 

Step 5: Run application

while tap on button you will see the Dialog.

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

1181 Views