MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.app.AlertDialog
import android.os.Bundle
import android.widget.*
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val context:MainActivity = this
// get the widgets reference from XML layout
val button = findViewById<Button>(R.id.button)
// create a builder for an alert dialog with theme resource
button.setOnClickListener {
val builder = AlertDialog.Builder(
context,
android.R.style.Theme_Material_Light_NoActionBar_Fullscreen
)
builder.setTitle("Full Screen Dialog Title")
builder.setMessage("\n\nThis is an example message." +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nSecond Text." +
"\n\n\n\n\n\n\n\n\nThird Text." +
"\n\n\n\n\n\n\n\n\n\n\n\n\n\nFourth Text.")
builder.setPositiveButton("Yes"){ dialog,which->
// do something on positive button click
}
builder.setNegativeButton("No"){dialog,which->
// do something when negative button clicked
}
builder.setNeutralButton("Cancel"){dialog,which->
// do something on neutral button click
}
val dialog = builder.create()
dialog.show()
}
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
android:background="#DCDCDC"
android:padding="24dp">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Full Screen Alert Dialog"
android:textAllCaps="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
build.gradle dependencies[add]
// Material components
implementation 'com.google.android.material:material:1.6.1'