In this project, I used two alert dialog, one with single positive button and other with positive, negative and neutral button.
Project URL: https://gitlab.com/kotlin-samples/AlertDialog-Sample
activity_main.xml
<?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=”.MainActivity”><Button
android:id=”@+id/alert1″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/call_alert_1″
app:layout_constraintBottom_toTopOf=”@+id/alert2″
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toTopOf=”parent” /><Button
android:id=”@+id/alert2″
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:text=”@string/call_alert_2″
app:layout_constraintBottom_toBottomOf=”parent”
app:layout_constraintLeft_toLeftOf=”parent”
app:layout_constraintRight_toRightOf=”parent”
app:layout_constraintTop_toBottomOf=”@+id/alert1″ /></android.support.constraint.ConstraintLayout>
MainActivity.kt
package com.mathi.alertdialogsample
import android.content.DialogInterface
import android.os.Bundle
import android.support.v7.app.AlertDialog
import android.support.v7.app.AppCompatActivity
import android.view.View
import android.widget.Button
import android.widget.Toastclass MainActivity : AppCompatActivity() {
internal var builder: AlertDialog.Builder? = nullinternal var alert1: Button? = null
internal var alert2: Button? = nulloverride fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)alert1 = findViewById(R.id.alert1) as Button
alert2 = findViewById(R.id.alert2) as Buttonalert1!!.setOnClickListener(alert1OnClickListener)
alert2!!.setOnClickListener(alert2OnClickListener)builder = AlertDialog.Builder(this@MainActivity)
}private val alert1OnClickListener = View.OnClickListener { showAlert1() }
private val alert2OnClickListener = View.OnClickListener { showAlert2() }
private fun showAlert1() {
builder!!.setTitle(getText(R.string.alert_1))
builder!!.setMessage(getText(R.string.alert_1_msg))
builder!!.setIcon(R.drawable.ic_done)
builder!!.setPositiveButton(getText(R.string.ok), okOnClickListener)
builder!!.show()
}private val okOnClickListener = DialogInterface.OnClickListener { dialog, which -> Toast.makeText(this@MainActivity, getText(R.string.you_pressed_ok), Toast.LENGTH_SHORT).show() }
private fun showAlert2() {
builder!!.setTitle(getText(R.string.alert_2))
builder!!.setMessage(getText(R.string.alert_2_msg))
builder!!.setIcon(R.drawable.ic_done)
builder!!.setPositiveButton(getText(R.string.ok), okOnClickListener)
builder!!.setNegativeButton(getText(R.string.cancel), cancelOnClickListener)
builder!!.setNeutralButton(getText(R.string.close), closeOnClickListener)
builder!!.show()
}private val cancelOnClickListener = DialogInterface.OnClickListener { dialog, which -> Toast.makeText(this@MainActivity, getText(R.string.you_pressed_cancel), Toast.LENGTH_SHORT).show() }
private val closeOnClickListener = DialogInterface.OnClickListener { dialog, which -> Toast.makeText(this@MainActivity, getText(R.string.you_pressed_close), Toast.LENGTH_SHORT).show() }
}