Alert Builder In Android - Kotlin

Kotlin

In this article, we will learn how to work with Alerts in Android using Kotlin. If you are new to Kotlin read my previous articles. It is very basic but helpful for learning from scratch.

Implementing Alerts in Android using Kotlin is similar to Java implementation and so, it is very easy to understand. Here, we will learn the following alerts.

  1. Alert with two options
  2. Alert with list

Coding Part

I have split this article into 3 steps as follows:

Step 1 - Creating new Android Project with Kotlin in Android Studio.

Step 2 - Adding User Interface in your layout file.

Step 3 - Implementing the alerts in Android Application.

Step 1 - Creating new Android Project with Kotlin in Android Studio

By default, Android Studio 3.0 has the checkbox for Kotlin Support for your Android Application. Create new project in Android studio, check the Kotlin support, and start as usual with Android Studio 3.0. 

Kotlin

For migrating Java Android Project to Kotlin Android Project, you can do the following processes.

Configuring Kotlin

Kotlin support can be configured by selecting Tools - Kotlin - Configure Kotlin. Then, Click “Sync Now”. This step is applicable to the Android Studio versions prior to  3.0, so it's best to  update your Android Studio.

In Android Studio 3.0, by default you have Kotlin Activity. For Android Studio with versions lower than 3.0, you can convert Java activity into Kotlin Activity.

Open Quick Search or Click Ctrl + Shift + A for Windows Users and Search and Select “Convert Java to Kotlin” or simply Select Ctrl + Shift + Alt + K. 

Step 2 - Adding User Interface in your Layout file

In this step, we will add buttons for triggering normal alerts and list alerts. Open your activity_main.xml file and paste the following.

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     xmlns:app="http://schemas.android.com/apk/res-auto"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:layout_width="match_parent"  
  6.     android:layout_height="match_parent"  
  7.     tools:context="com.ajts.androidmads.kotlinalerts.MainActivity"  
  8.     tools:layout_editor_absoluteY="81dp">  
  9.   
  10.     <Button  
  11.         android:id="@+id/optionAlert"  
  12.         android:layout_width="0dp"  
  13.         android:layout_height="wrap_content"  
  14.         android:layout_marginTop="184dp"  
  15.         android:text="Normal Alert"  
  16.         app:layout_constraintEnd_toEndOf="@+id/listAlert"  
  17.         app:layout_constraintStart_toStartOf="@+id/listAlert"  
  18.         app:layout_constraintTop_toTopOf="parent"  
  19.         android:onClick="showNormalAlert"/>  
  20.   
  21.     <Button  
  22.         android:id="@+id/listAlert"  
  23.         android:layout_width="123dp"  
  24.         android:layout_height="wrap_content"  
  25.         android:layout_marginTop="7dp"  
  26.         android:text="List Alert"  
  27.         app:layout_constraintEnd_toEndOf="parent"  
  28.         app:layout_constraintStart_toStartOf="parent"  
  29.         app:layout_constraintTop_toBottomOf="@+id/optionAlert"  
  30.         android:onClick="showListAlert" />  
  31.   
  32. </android.support.constraint.ConstraintLayout>  

The Screen looks like the below screenshot.

Kotlin

Step 3 - Implementing the alerts in Kotlin

Now open your Activity file and in my case, I am opening “MainActivity.kt”.

Show Alert Dialog with two options.
  1. fun showNormalAlert(v: View){  
  2.     val dialog = AlertDialog.Builder(this).setTitle("Kotlin Study").setMessage("Alert Dialog")  
  3.             .setPositiveButton("Confirm", { dialog, i ->  
  4.                 Toast.makeText(applicationContext, "Hello Friends", Toast.LENGTH_LONG).show()  
  5.             })  
  6.             .setNegativeButton("Cancel", { dialog, i -> })  
  7.     dialog.show()  
Showing List Type Alert Dialog
  1. fun showListAlert(v: View){  
  2.     val items = arrayOf<CharSequence>("Android""iOS""Windows")  
  3.     val dialog = AlertDialog.Builder(this).setTitle("Select your Mobile OS").setItems(items,  
  4.             {  
  5.                 dialog, which ->  
  6.                 Toast.makeText(applicationContext, items[which], Toast.LENGTH_LONG).show();  
  7.             })  
  8.     dialog.show()  
  9. }  

Full Code

You can find the full code implementation below.

  1. package com.ajts.androidmads.kotlinalerts  
  2.   
  3. import android.support.v7.app.AppCompatActivity  
  4. import android.os.Bundle  
  5. import android.support.v7.app.AlertDialog  
  6. import android.view.View  
  7. import android.widget.Toast  
  8.   
  9. class MainActivity : AppCompatActivity() {  
  10.   
  11.     override fun onCreate(savedInstanceState: Bundle?) {  
  12.         super.onCreate(savedInstanceState)  
  13.         setContentView(R.layout.activity_main)  
  14.     }  
  15.   
  16.     fun showNormalAlert(v: View){  
  17.         val dialog = AlertDialog.Builder(this).setTitle("Kotlin Study").setMessage("Alert Dialog")  
  18.                 .setPositiveButton("Confirm", { dialog, i ->  
  19.                     Toast.makeText(this@MainActivity, "Hello Friends", Toast.LENGTH_LONG).show()  
  20.                 })  
  21.                 .setNegativeButton("Cancel", { dialog, i -> })  
  22.         dialog.show()  
  23.     }  
  24.   
  25.     fun showListAlert(v: View){  
  26.         val items = arrayOf<CharSequence>("Android""iOS""Windows")  
  27.         val dialog = AlertDialog.Builder(this).setTitle("Select your Mobile OS").setItems(items,  
  28.                 {  
  29.                     dialog, which ->  
  30.                     Toast.makeText(applicationContext, items[which], Toast.LENGTH_LONG).show();  
  31.                 })  
  32.         dialog.show()  
  33.     }  
  34. }  

If this article is useful to you, do like it and keep on commenting if you have any clarifications.