How to Create an Alert Dialog Box in Android?

Alert dialog shows the alert message and gives the answer in the form yes or no. Alert dialog displays the message on mobile application as per its response. For example when a user logs out while displaying one dialog “Are you sure to logout ?”

Alert dialog code has three methods:

  1. setTitle()  method for displaying the alert dialog box title.
  2. setMessage() method for displaying message
  3. setIcon() method is used to set an icon on the alert dialog box.



Step by step Implementation process:

Step 1: Create a new project in Android  or you can use an already created project.  


Step 2: Create an activity_main.xml file in your layout folder . path (res>layout)

 implement the same invoke the following code inside 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/idRLContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">

<TextView
android:id="@+id/txtTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="28dp"
android:text="Candidroot solutions"
android:textAlignment="center"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.842"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

<Button
android:id="@+id/btndialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="292dp"
android:layout_marginStart="30dp"
android:layout_marginEnd="30dp"
android:text="click to show alert dialog"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtTitle" />

</androidx.constraintlayout.widget.ConstraintLayout>

Step 3: Create an MainActivity.kt file in your package folder . 

 implement the same invoke the following code inside MainActivity.kt file.

 import android.content.DialogInterface
import android.os.Bundle
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity

class MainActivityone : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main_activityone)
val btndialog = findViewById<Button>(R.id.btndialog)
btndialog.setOnClickListener {
showdialogdemo()
}

}

@Deprecated("This method has been deprecated in favor of using the\n {@link OnBackPressedDispatcher} via {@link #getOnBackPressedDispatcher()}.\n The OnBackPressedDispatcher controls how back button events are dispatched\n to one or more {@link OnBackPressedCallback} objects.")
fun showdialogdemo() {

//Create object of alertdialog builder
val builder: android.app.AlertDialog.Builder =
android.app.AlertDialog.Builder(this@MainActivityone)

//created object set message
builder.setMessage("Do you want to Logout ?")

//created object set title
builder.setTitle("Alert !")

//created object set cancelable
builder.setCancelable(false)

//set yes button in alert dialog
builder.setPositiveButton(
"Yes"
) { dialog: DialogInterface?, which: Int ->
finish()
}

//set no button in alert dialog
builder.setNegativeButton(
"No"
) { dialog: DialogInterface, which: Int ->
dialog.cancel()
}

val alertDialog: android.app.AlertDialog? = builder.create()

alertDialog?.show()
}
}
Step 4: Output of above example


Happy coding!

365Bloggy May 24, 2024
Share this post
Tags
SUBSCRIBE THIS FORM


Archive