{"id":2319,"date":"2017-03-18T09:10:50","date_gmt":"2017-03-18T09:10:50","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?page_id=2319"},"modified":"2019-06-14T10:34:47","modified_gmt":"2019-06-14T10:34:47","slug":"alertdialog","status":"publish","type":"page","link":"https:\/\/abhiandroid.com\/ui\/alertdialog","title":{"rendered":"Alert Dialog Tutorial With Example In Android Studio"},"content":{"rendered":"<p>Alert Dialog in an android UI prompts a small window to make decision on mobile screen. Sometimes before making a decision it is required to give an alert to the user without moving to next activity. To solve this problem alert dialog came into practise. For example you have seen this type of alert when you try to exit the App and App ask you to confirm exiting.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2346\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Example-In-Android-Studio-2.png\" alt=\"Alert Dialog Example In Android Studio\" width=\"277\" height=\"122\" \/><\/p>\n<hr \/>\n<h4><strong>AlertDialog.Builder Components Used In Alert Dialog<\/strong><\/h4>\n<p>AlertDialog.Builder is used to create an interface for Alert Dialog in Android for\u00a0setting like alert title, message, image, button, button onclick functionality etc.<\/p>\n<pre>AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2323\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Components.png\" alt=\"Alert Dialog Components\" width=\"277\" height=\"448\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Components.png 277w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Components-185x300.png 185w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/>Below\u00a0are the components of Alert Dialog:<\/p>\n<p><span style=\"color: #008000;\"><strong>1. setTitle(CharSequence title) &#8211;<\/strong><\/span> This component is used to set the title of the alert dialog. It is optional component.<\/p>\n<pre>  \/\/ Setting Alert Dialog Title\r\n        alertDialogBuilder.setTitle(\"Confirm Exit..!!!\");<\/pre>\n<p><strong><span style=\"color: #008000;\">2. setIcon(Drawable icon) &#8211;<\/span><\/strong> This component add icon before the title. You will need to save image in drawable icon.<\/p>\n<pre> \/\/ Icon Of Alert Dialog\r\n        alertDialogBuilder.setIcon(R.drawable.question);<\/pre>\n<p><span style=\"color: #008000;\"><strong>3. setMessage(CharSequence message)<\/strong><\/span> &#8211; This component displays the required message in the alert dialog.<\/p>\n<pre>  \/\/ Setting Alert Dialog Message\r\n        alertDialogBuilder.setMessage(\"Are you sure,You want to exit\");<\/pre>\n<p><span style=\"color: #008000;\"><strong>4. setCancelable(boolean cancelable) &#8211;<\/strong> <\/span>This component has boolean value i.e true\/false. If set to false it allows to cancel the dialog box by clicking on area outside the dialog else it allows.<\/p>\n<pre>  alertDialogBuilder.setCancelable(false);<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2362\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Component-In-Android-Studio.png\" alt=\"Custom Alert Dialog Component In Android Studio\" width=\"277\" height=\"448\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Component-In-Android-Studio.png 277w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Component-In-Android-Studio-185x300.png 185w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/><br \/>\n<strong><span style=\"color: #008000;\">5. setPositiveButton(CharSequence text, DialogInterface.OnClickListener listener)<\/span> <\/strong>&#8211; This component add positive button and further with this user confirm he wants the alert dialog question to happen.<\/p>\n<pre>  alertDialogBuilder.setPositiveButton(\"yes\", new DialogInterface.OnClickListener() {\r\n\r\n                            @Override\r\n                            public void onClick(DialogInterface arg0, int arg1) {\r\n                                finish();\r\n                            }\r\n                        });<\/pre>\n<p><strong><span style=\"color: #008000;\">6. setNegativeButton(CharSequence text, DialogInterface.OnClickListener listener) &#8211;<\/span> <\/strong>This component add negative button and further with this user confirm he doesn&#8217;t want the alert dialog question to happen.<\/p>\n<pre>alertDialogBuilder.setNegativeButton(\"No\", new DialogInterface.OnClickListener() {\r\n                            @Override\r\n                            public void onClick(DialogInterface dialog, int which) {\r\n                                Toast.makeText(MainActivity.this,\"You clicked over No\",Toast.LENGTH_SHORT).show();\r\n                            }\r\n                        });<\/pre>\n<p><span style=\"color: #008000;\"><strong>7. setNeutralButton(CharSequence text, DialogInterface.OnClickListener listener) &#8211;<\/strong><\/span> This component simply add a new button and on this button\u00a0developer can set any other onclick functionality like cancel button on alert dialog.<\/p>\n<pre> alertDialogBuilder.setNeutralButton(\"Cancel\", new DialogInterface.OnClickListener() {\r\n                            @Override\r\n                            public void onClick(DialogInterface dialog, int which) {\r\n                                Toast.makeText(getApplicationContext(),\"You clicked on Cancel\",Toast.LENGTH_SHORT).show();\r\n                            }\r\n                         });<\/pre>\n<hr \/>\n<h4><strong>Alert Dialog Example In Android Studio<\/strong><\/h4>\n<p>Below is the example of Alert Dialog in which the functionality of Alert Dialog is defined over button click. In this example we have used a simple button and on\u00a0that button click the alert dialog window will appear.<\/p>\n<p>Below you can download code, see final output and step by step explanation of Alert Dialog example in Android Studio.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"http:\/\/www.mediafire.com\/file\/oooz5t23kjdmoi7\/AlertDialogExample.zip\/file\" target=\"_blank\" rel=\"nofollow noopener\">Download Code<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2321\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Example-In-Android-Studio.png\" alt=\"Alert Dialog Example In Android Studio\" width=\"277\" height=\"448\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Example-In-Android-Studio.png 277w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Example-In-Android-Studio-185x300.png 185w\" sizes=\"auto, (max-width: 277px) 100vw, 277px\" \/><br \/>\n<span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>AlertDialogExample<\/strong>.<\/p>\n<p><span style=\"color: #008000;\"><strong>Step 2:<\/strong><\/span> Open <span style=\"color: #008000;\">res -&gt; layout -&gt; activity_main.xml (or) main.xml<\/span> and add following code:<\/p>\n<p>Here we add the button UI on\u00a0click of which alert dialog will appear. We also used textview for asking user to click on button.<\/p>\n<pre>&lt;?xml version=\"1.0\" encoding=\"utf-8\"?&gt;\r\n&lt;RelativeLayout xmlns:android=\"http:\/\/schemas.android.com\/apk\/res\/android\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:id=\"@+id\/activity_main\"\r\n    android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"\r\n    android:paddingBottom=\"@dimen\/activity_vertical_margin\"\r\n    android:paddingLeft=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingRight=\"@dimen\/activity_horizontal_margin\"\r\n    android:paddingTop=\"@dimen\/activity_vertical_margin\"\r\n    tools:context=\"com.example.alertdialogexample.MainActivity\"&gt;\r\n\r\n    &lt;Button\r\n        android:text=\"@string\/exit\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:id=\"@+id\/button\"\r\n        android:onClick=\"exit\"\r\n        android:textStyle=\"normal|bold\" \r\n        style=\"@style\/Widget.AppCompat.Button.Colored\"\r\n        android:layout_alignParentBottom=\"true\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginBottom=\"131dp\"\/&gt;\r\n\r\n    &lt;TextView\r\n        android:text=\"@string\/click_over_button_to_exit\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_marginBottom=\"68dp\"\r\n        android:id=\"@+id\/textView2\"\r\n        android:layout_above=\"@+id\/button\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:textSize=\"18sp\"\r\n        android:textStyle=\"normal|bold\"\r\n        android:gravity=\"center\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3 :<\/strong><\/span> Now open <span style=\"color: #008000;\">app -&gt; java -&gt; package -&gt; MainActivity.java<\/span> and add the below code.<\/p>\n<p>In this step firstly AlertDialog.Builder is used to create a interface like setting alert title, message, image, button, button onclick functionality etc.<\/p>\n<p><span style=\"color: #ff0000;\"><strong>Important Note:<\/strong> <\/span>Add a image to the drawable folder before setting the alert dialog icon.<\/p>\n<pre>package com.example.alertdialogexample;\r\n\r\nimport android.content.DialogInterface;\r\nimport android.support.v7.app.AlertDialog;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Toast;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n\r\n    @Override\r\n    protected void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n    }\r\n    public void exit(View view){\r\n        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);\r\n        \/\/ Setting Alert Dialog Title\r\n        alertDialogBuilder.setTitle(\"Confirm Exit..!!!\");\r\n        \/\/ Icon Of Alert Dialog \r\n        alertDialogBuilder.setIcon(R.drawable.question);\r\n        \/\/ Setting Alert Dialog Message\r\n        alertDialogBuilder.setMessage(\"Are you sure,You want to exit\");\r\n        alertDialogBuilder.setCancelable(false);\r\n\r\n                alertDialogBuilder.setPositiveButton(\"Yes\", new DialogInterface.OnClickListener() {\r\n\r\n                            @Override\r\n                            public void onClick(DialogInterface arg0, int arg1) {\r\n                                finish();\r\n                            }\r\n                        });\r\n\r\n                alertDialogBuilder.setNegativeButton(\"No\", new DialogInterface.OnClickListener() {\r\n                            @Override\r\n                            public void onClick(DialogInterface dialog, int which) {\r\n                                Toast.makeText(MainActivity.this,\"You clicked over No\",Toast.LENGTH_SHORT).show();\r\n                            }\r\n                        });\r\n                alertDialogBuilder.setNeutralButton(\"Cancel\", new DialogInterface.OnClickListener() {\r\n                            @Override\r\n                            public void onClick(DialogInterface dialog, int which) {\r\n                                Toast.makeText(getApplicationContext(),\"You clicked on Cancel\",Toast.LENGTH_SHORT).show();\r\n                            }\r\n                         });\r\n\r\n        AlertDialog alertDialog = alertDialogBuilder.create();\r\n        alertDialog.show();\r\n    }\r\n}\r\n<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and\u00a0click on the button. The alert dialog will appear asking user to confirm if he wants to exit App. If user click on &#8216;No&#8217;, he remains in the App and if he click on &#8216;Yes&#8217; the user will get exit from the App.<\/p>\n<hr \/>\n<h4><strong>Custom Alert Dialog:<\/strong><\/h4>\n<p>The custom dialog uses DIALOG to create custom alert in android studio. Dialog display a small window i.e a popup which draws the user attention over the activity before they continue moving forward. For more details read <a href=\"\/ui\/custom-alert-dialog-example-android-studio.html\">custom dialog tutorial<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2382\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Vs-Custom-Alert-Dialog-in-Android.jpg\" alt=\"Alert Dialog Vs Custom Alert Dialog in Android\" width=\"514\" height=\"188\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Vs-Custom-Alert-Dialog-in-Android.jpg 514w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Alert-Dialog-Vs-Custom-Alert-Dialog-in-Android-300x110.jpg 300w\" sizes=\"auto, (max-width: 514px) 100vw, 514px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Alert Dialog in an android UI prompts a small window to make decision on mobile screen. Sometimes before making a decision it is required to give an alert to the user without moving to next activity. To solve this problem alert dialog came into practise. For example you have seen this type of alert when &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/alertdialog\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Alert Dialog Tutorial With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"open","ping_status":"closed","template":"home.php","meta":{"_acf_changed":false,"footnotes":""},"class_list":["post-2319","page","type-page","status-publish","hentry"],"acf":[],"psp_head":"<title>Alert Dialog Tutorial With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"Learn how to use alert dialog with example in Android Studio. Here we also explain the Alertdialog Builder components in details using source code.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/alertdialog\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2319","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/comments?post=2319"}],"version-history":[{"count":3,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2319\/revisions"}],"predecessor-version":[{"id":2842,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/pages\/2319\/revisions\/2842"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2319"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}