Pages

Wednesday, June 7, 2017

Alertdialog in Android using Kotlin

In this Post, I show you how to display an alert dialog in android using Kotlin and Anko library. In this Demo app will create simple Alert and Alert with Action Button.

Application developing tools :

Android Studio 3.0 Canary 3
Android 25 version
Build Tools Version - 25.0.2
Kotlin Version - 1.1.2-4
Anko Library Version - 0.10.1

Follow Steps :
1. Create New Application
2. Apply plugin and Add dependency in build.gradle file
3. Add button in layout file
4. Add code in Activity

Kotlin Android Extensions plugin (automatically bundled into the Kotlin plugin in Android Studio) solves the issue: replacing findViewById with a brief and straightforward code.

Anko is a Kotlin library which makes Android application development faster and easier. It makes your code clean and easy to read, and lets you forget about rough edges of the Android SDK for Java.
we using Anko Commons: a lightweight library full of helpers for intents, dialogs, logging and so on;.

In General, Simple Text Alert with one button.

 val simpleAlert = AlertDialog.Builder(this@MainActivity).create()
        simpleAlert.setTitle("Alert")
        simpleAlert.setMessage("Show simple Alert")

        simpleAlert.setButton(AlertDialog.BUTTON_POSITIVE, "OK", {
            dialogInterface, i ->
            Toast.makeText(applicationContext, "You clicked on OK", Toast.LENGTH_SHORT).show()
        })

        simpleAlert.show()

Anko Library provide simple way to show simple text Alert with one button.
 alert("Show simple Alert","Alert") {
            positiveButton("OK") {
                toast("You clicked on OK")
            }
        }.show()


let's start!

Create New Application






appyly plugin and add dependency in app-module build.gradle file

 

Apply Plugin
apply plugin: 'kotlin-android-extensions'
Add Dependency (Anko Commons Library)
    compile "org.jetbrains.anko:anko-commons:0.10.1"

Layout Files : main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:padding="10dp"
    tools:context="com.limbani.alertdialogdemo.MainActivity">

    <Button
        android:id="@+id/simpleAlert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Simple Alert" />

    <Button
        android:id="@+id/alertTwoButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Alert with two Button" />

    <Button
        android:id="@+id/alertThreeButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Alert with three Button" />

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:gravity="center"
        android:text="Using Anko Library Example" />

    <Button
        android:id="@+id/ankoSimpleAlert"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Simple Alert" />

    <Button
        android:id="@+id/ankoAlertTwoButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Alert with two Button" />

    <Button
        android:id="@+id/ankoAlertThreeButton"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Alert with three Button" />
</LinearLayout>

Activity :


MainActivity.kt here first three button show to simple way to show alert dialog and below three button show the alert dialog using Anko Library
package com.limbani.alertdialogdemo

import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.widget.Toast
import kotlinx.android.synthetic.main.activity_main.*
import org.jetbrains.anko.alert
import org.jetbrains.anko.toast

class MainActivity : AppCompatActivity() {

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

        simpleAlert.setOnClickListener {
            showSimpleAlert()
        }

        alertTwoButton.setOnClickListener {
            showAlertWithTwoButton()
        }

        alertThreeButton.setOnClickListener {
            showAlertWithThreeButton()
        }

        //Anko Example Button here
        ankoSimpleAlert.setOnClickListener {
            ankoShowSimpleAlert()
        }

        ankoAlertTwoButton.setOnClickListener {
            ankoShowAlertWithTwoButton()
        }

        ankoAlertThreeButton.setOnClickListener {
            ankoShowAlertWithThreeButton()
        }
    }

    //Anko Library Example code here
    private fun ankoShowAlertWithThreeButton() {
        alert("Show Alert with three Button", "Alert") {
            positiveButton("POSITIVE") {
                toast("You clicked on POSITIVE Button")
            }
            negativeButton("NEGATIVE") {
                toast("You clicked on NEGATIVE Button")
            }
            neutralPressed("NEUTRAL") {
                toast("You clicked on NEUTRAL Button")
            }
        }.show()
    }

    private fun ankoShowAlertWithTwoButton() {
        alert("how Alert with two Button","Alert") {
            positiveButton("YES") {
                toast("You clicked on YES")
            }
            negativeButton("NO") {
                toast("You clicked on NO")
            }
        }.show()
    }

    private fun ankoShowSimpleAlert() {
        alert("Show simple Alert","Alert") {
            positiveButton("OK") {
                toast("You clicked on OK")
            }
        }.show()
    }

    //General code here
    private fun showAlertWithThreeButton() {
        val alertDilog = AlertDialog.Builder(this@MainActivity).create()
        alertDilog.setTitle("Alert")
        alertDilog.setMessage("Show Alert with three Button")

        alertDilog.setButton(AlertDialog.BUTTON_POSITIVE, "POSITIVE", {
            dialogInterface, i ->
            Toast.makeText(applicationContext, "You clicked on POSITIVE Button", Toast.LENGTH_SHORT).show()
        })

        alertDilog.setButton(AlertDialog.BUTTON_NEGATIVE, "NEGATIVE", {
            dialogInterface, j ->
            Toast.makeText(applicationContext, "You clicked on NEGATIVE Button", Toast.LENGTH_SHORT).show()
        })
        alertDilog.setButton(AlertDialog.BUTTON_NEUTRAL, "NEUTRAL", {
            dialogInterface, k ->
            Toast.makeText(applicationContext, "You clicked on NEUTRAL Button", Toast.LENGTH_SHORT).show()
        })

        alertDilog.show()
    }

    private fun showAlertWithTwoButton() {
        val alertDilog = AlertDialog.Builder(this@MainActivity).create()
        alertDilog.setTitle("Alert")
        alertDilog.setMessage("Show Alert with two Button")

        alertDilog.setButton(AlertDialog.BUTTON_POSITIVE, "YES", {
            dialogInterface, i ->
            Toast.makeText(applicationContext, "You clicked on YES", Toast.LENGTH_SHORT).show()
        })

        alertDilog.setButton(AlertDialog.BUTTON_NEGATIVE, "NO", {
            dialogInterface, i ->
            Toast.makeText(applicationContext, "You clicked on NO", Toast.LENGTH_SHORT).show()
        })

        alertDilog.show()
    }

    private fun showSimpleAlert() {

        val simpleAlert = AlertDialog.Builder(this@MainActivity).create()
        simpleAlert.setTitle("Alert")
        simpleAlert.setMessage("Show simple Alert")

        simpleAlert.setButton(AlertDialog.BUTTON_POSITIVE, "OK", {
            dialogInterface, i ->
            Toast.makeText(applicationContext, "You clicked on OK", Toast.LENGTH_SHORT).show()
        })

        simpleAlert.show()
    }
}

Test (Screenshot)




Resource
https://kotlinlang.org
Android Developer Blog
Anko Library
 
Thanks for reading this post.
Enjoy:)

2 comments: