Monday, October 24, 2022

How to Create an Alert Dialog Box in Android?

 import android.content.DialogInterface;

import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;

import androidx.appcompat.app.AppCompatActivity;


public class MainActivity extends AppCompatActivity {


@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

}


// Declare the onBackPressed method when the back button is pressed this method will call

@Override

public void onBackPressed() {

// Create the object of AlertDialog Builder class

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);


// Set the message show for the Alert time

builder.setMessage("Do you want to exit ?");


// Set Alert Title

builder.setTitle("Alert !");


// Set Cancelable false for when the user clicks on the outside the Dialog Box then it will remain show

builder.setCancelable(false);


// Set the positive button with yes name Lambda OnClickListener method is use of DialogInterface interface.

builder.setPositiveButton("Yes", (DialogInterface.OnClickListener) (dialog, which) -> {

// When the user click yes button then app will close

finish();

});


// Set the Negative button with No name Lambda OnClickListener method is use of DialogInterface interface.

builder.setNegativeButton("No", (DialogInterface.OnClickListener) (dialog, which) -> {

// If user click no then dialog box is canceled.

dialog.cancel();

});


// Create the Alert dialog

AlertDialog alertDialog = builder.create();

// Show the Alert Dialog box

alertDialog.show();

}

}


No comments:

Post a Comment