Simple Android Alert Dialog
https://www.viralandroid.com/2015/09/android-alert-dialog-example.html
A dialog box is used to display message to the users, to ask users to enter some information and to take decision. In this post, I have created a simple android alert dialog box which shows a title of dialog, a text message and a button. Displaying an alert dialog is very easy but if you are beginner android developer, you may feel difficult for the first time.
You can also display your custom design alert dialog. Android Custom Alert Dialog Example gives you instructions to display custom alert dialog in your app. With custom alert dialog, you can easily use graphic and own design using xml like other layout.
To display simple alert dialog box to your android application, simply use following java code in your onClick method.
res/layout/activity_main.xml
Here is modified code of java activity file.
src/ActivityMain.java
Now, run your application and click Show Simple Android Alert Dialog button. Your application contains a dialog with some text message and a button like this.
You can also display your custom design alert dialog. Android Custom Alert Dialog Example gives you instructions to display custom alert dialog in your app. With custom alert dialog, you can easily use graphic and own design using xml like other layout.
How to Display Simple Android Alert Dialog Box
To display simple alert dialog box to your android application, simply use following java code in your onClick method.
Here is modified code of activity layout file.AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create(); alertDialog.setTitle("Alert Dialog Title"); alertDialog.setMessage("Here is android alert dialog message"); // Alert dialog button alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int which) { // Alert dialog action goes here // onClick button code here dialog.dismiss();// use dismiss to cancel alert dialog } }); alertDialog.show();
res/layout/activity_main.xml
Here is modified code of java activity file.
src/ActivityMain.java
Now, run your application and click Show Simple Android Alert Dialog button. Your application contains a dialog with some text message and a button like this.
Alert Dialog Example |