Showing posts with label Kotlin Alert. Show all posts

Alert Builder in Android -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. This article is very basic. But very worthier 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:

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.


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 pre versions of 3.0. Best way is you must update your Android Studio.

In Android Studio 3.0, by default you have Kotlin Activity. For Android Studio with Version less than 3.0, can convert their 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.

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.
<?xml version="1.0" encoding="utf-8"?>  
<android.support.constraint.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="com.ajts.androidmads.kotlinalerts.MainActivity"  
    tools:layout_editor_absoluteY="81dp">  
  
    <Button  
        android:id="@+id/optionAlert"  
        android:layout_width="0dp"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="184dp"  
        android:text="Normal Alert"  
        app:layout_constraintEnd_toEndOf="@+id/listAlert"  
        app:layout_constraintStart_toStartOf="@+id/listAlert"  
        app:layout_constraintTop_toTopOf="parent"  
        android:onClick="showNormalAlert"/>  
  
    <Button  
        android:id="@+id/listAlert"  
        android:layout_width="123dp"  
        android:layout_height="wrap_content"  
        android:layout_marginTop="7dp"  
        android:text="List Alert"  
        app:layout_constraintEnd_toEndOf="parent"  
        app:layout_constraintStart_toStartOf="parent"  
        app:layout_constraintTop_toBottomOf="@+id/optionAlert"  
        android:onClick="showListAlert" />  
  
</android.support.constraint.ConstraintLayout>  
The Screen looks like the below screenshot.


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
fun showNormalAlert(v: View){  
    val dialog = AlertDialog.Builder(this).setTitle("Kotlin Study").setMessage("Alert Dialog")  
            .setPositiveButton("Confirm", { dialog, i ->  
                Toast.makeText(applicationContext, "Hello Friends", Toast.LENGTH_LONG).show()  
            })  
            .setNegativeButton("Cancel", { dialog, i -> })  
    dialog.show()  
} 
Showing List Type Alert Dialog
fun showListAlert(v: View){  
    val items = arrayOf("Android", "iOS", "Windows")  
    val dialog = AlertDialog.Builder(this).setTitle("Select your Mobile OS").setItems(items,  
            {  
                dialog, which ->  
                Toast.makeText(applicationContext, items[which], Toast.LENGTH_LONG).show();  
            })  
    dialog.show()  
}

Full Code

You can find the full code implementation below.
package com.ajts.androidmads.kotlinalerts  
  
import android.support.v7.app.AppCompatActivity  
import android.os.Bundle  
import android.support.v7.app.AlertDialog  
import android.view.View  
import android.widget.Toast  
  
class MainActivity : AppCompatActivity() {  
  
    override fun onCreate(savedInstanceState: Bundle?) {  
        super.onCreate(savedInstanceState)  
        setContentView(R.layout.activity_main)  
    }  
  
    fun showNormalAlert(v: View){  
        val dialog = AlertDialog.Builder(this).setTitle("Kotlin Study").setMessage("Alert Dialog")  
                .setPositiveButton("Confirm", { dialog, i ->  
                    Toast.makeText(this@MainActivity, "Hello Friends", Toast.LENGTH_LONG).show()  
                })  
                .setNegativeButton("Cancel", { dialog, i -> })  
        dialog.show()  
    }  
  
    fun showListAlert(v: View){  
        val items = arrayOf("Android", "iOS", "Windows")  
        val dialog = AlertDialog.Builder(this).setTitle("Select your Mobile OS").setItems(items,  
                {  
                    dialog, which ->  
                    Toast.makeText(applicationContext, items[which], Toast.LENGTH_LONG).show();  
                })  
        dialog.show()  
    }  
}
If this article is useful to you, do like it and keep on commenting if you have any clarifications.

Download Code: 

You can download sample code from the following GitHub link.