How to display an Alert Dialog on Android
Alert Dialog Box is used to alert the user for some action, for example by clicking some button, the user can set an alert for anything such as application closed or not.
Please follow the steps to use Alert Dialog box on your Android activity:
Step 1) So let’s get started first we will take a button to open this Alert Dialog and change the name of the button for example as Alert.
Step 2) Now go to my MainActivity.java and create a button variable.
Step 3) Now create a method which will be called onButton click event and to cast the button variable by assigning an ID to it.
Step 4) Now use the button to set an onClick Event Function and inside the function, we will create a new onClickListener
Step 5) Now inside this onClick Listener creates an Alert dialog. So let’s create Alert Dialog and here we named it as A Builder. To create Alert Dialog Box on this activity, we just have assigned it to this class.
Step 6) And now create a message for the builder by calling Set Message method on this builder and inside the Set Message we can, call some message for example we can display a message "Do you want to close this app" and also if user want he can close it so we will provide some method to this alert box for example set cancellable and as this alert box should not be cancellable so we will just set it as false.
Step 7) Now we are going to set the positive button, for example, yes if in condition if that condition arises that we want to display yes or no condition button on this dialog box then how we can do it. This positive button takes some arguments,the first argument it takes is the title or text of this yes button, And the second argument which it takes is the OnClickListener because of its button.
Step 8) The second argument OnClickListener will create a new OnClick method automatically where we just need to call finish. This Finish method just closes your app gracefully, so if the user wants to close the app he can click the CS button and close the app gracefully.
Step 9) Otherwise just go outside the set Positive button and set Negative button, we can also set negative condition . And the first argument of this is the text on this button, the second argument is the OnClick method, so here also a new OnClickListener where we can call a method to cancel this dialog box. Please just give a semi-colon outside negative button method in order to close the method.
Step 10) Just below this Builder method or Builder, create an Alert Dialog now. From this alert dialog instance we can set the title on this alert box, and then call show method to show this alert dialog box.
Step 11) Now call this method inside our On Create method, therefore just paste this method inside your onCreate method.
Step 12) So now the application will be running, there is an alert button and on clicking the alert button a new Alert dialog box appears. The title of this alert is "Alert" as set by us and there will also a message which we have set, "Do you want to close this app" And there are buttons no and yes. For example, click No button, nothing happens, just this Alert dialog box closes.And now when we click yes, it gracefully closes my app.
So in this way you can use Alert Dialog in your Android Activity.