MainActivity.kt
package com.cfsuman.kotlintutorials
import android.app.Activity
import android.os.Bundle
import android.text.Layout
import android.text.SpannableString
import android.text.style.AlignmentSpan
import android.widget.*
import androidx.appcompat.app.AlertDialog
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)
button.setOnClickListener {
val builder = AlertDialog.Builder(context)
val title = SpannableString("Alert dialog title")
// alert dialog title align center
title.setSpan(
AlignmentSpan.Standard(Layout.Alignment.ALIGN_CENTER),
0,
title.length,
0
)
// dialog title
builder.setTitle(title)
// dialog message
builder.setMessage(
"Lorem ipsum dolor sit amet, consectetur adipiscing elit." +
" Integer ultricies ligula nec nibh ullamcorper," +
" ac porta tellus dignissim. Nam in posuere elit." +
" Sed suscipit nulla a egestas consequat."
)
// alert dialog buttons
builder.setPositiveButton("OK",null)
builder.setNegativeButton("No",null)
builder.setNeutralButton("Cancel",null)
// finally, create the alert dialog and show it
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 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>

- kotlin - AlertDialog title text color size bold programmatically
- kotlin - AlertDialog EditText programmatically
- kotlin - AlertDialog single choice items
- kotlin - AlertDialog multiple choice
- kotlin - Material button remove padding
- kotlin - Material button center icon only
- kotlin - MaterialCardView border color width radius
- kotlin - MaterialCardView corner radius programmatically
- kotlin - ListView remove item programmatically
- kotlin - ListView remove divider line