Monday, March 2, 2020

android kotlin - Full screen AlertDialog example

MainActivity.kt

package com.example.jetpack

import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.appcompat.app.AppCompatActivity
import kotlinx.android.synthetic.main.activity_main.*


class MainActivity : AppCompatActivity() {

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

        val context = this

        // 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("Dialog Title")
            builder.setMessage("This is an example message." +
                    "\n\n\n\n\n\n\n\n\n\n\n\n\n\nAnother line." +
                    "\n\n\n\n\n\n\n\n\nAnother line." +
                    "\n\n\n\n\n\n\n\n\n\n\n\n\n\nAnother line.")

            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:id="@+id/constraintLayout"
    android:background="#F0FFFF"
    tools:context=".MainActivity">

    <com.google.android.material.button.MaterialButton
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Show Dialog"
        android:backgroundTint="#A4C639"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
res/values/styles.xml [update]

<resources>

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.MaterialComponents.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    </style>

</resources>
build.gradle dependencies[add]

// Material components theme
implementation 'com.google.android.material:material:1.1.0'
More android examples