Android Custom Dialog Popup kotlin

07-Jan-2025

Android Custom Dialog Popup kotlin

Output:







1/  Create and Modify custom_dialog.xml file.



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">

<TextView
android:id="@+id/dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Dialog"
android:textSize="18sp"
android:textStyle="bold"
android:layout_gravity="center_horizontal" />

<EditText
android:layout_marginVertical="24dp"
android:id="@+id/dialogInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter some text" />

<Button
android:id="@+id/dialogButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Submit" />
</LinearLayout>



2/  Modify activity_main.xml file.



<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">

<Button
android:id="@+id/showDialogue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show custom dialogue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>




3/  Modify the MainActivity.kt file



package com.microappvalley.androidcustomdialogpopupkotlin

import android.app.Dialog
import android.os.Bundle
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat

class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContentView(R.layout.activity_main)
ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.main)) { v, insets ->
val systemBars = insets.getInsets(WindowInsetsCompat.Type.systemBars())
v.setPadding(systemBars.left, systemBars.top, systemBars.right, systemBars.bottom)
insets
}

val showDialogBtn: Button = findViewById(R.id.showDialogue)

showDialogBtn.setOnClickListener {showCustomDialog()}
}

private fun showCustomDialog() {
val dialog = Dialog(this)
dialog.setContentView(R.layout.custom_dialog)
dialog.setCancelable(true)

dialog.window?.setLayout(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
val dialogTitle: TextView = dialog.findViewById(R.id.dialogTitle)
val dialogInput: EditText = dialog.findViewById(R.id.dialogInput)
val dialogButton: Button = dialog.findViewById(R.id.dialogButton)

dialogButton.setOnClickListener {
val inputText = dialogInput.text.toString()
if (inputText.isNotEmpty()) {
dialogTitle.text = "You entered: $inputText"
}
dialog.dismiss()
}

dialog.show()
}
}




Comments