{"id":2378,"date":"2017-03-21T06:18:08","date_gmt":"2017-03-21T06:18:08","guid":{"rendered":"http:\/\/abhiandroid.com\/ui\/?p=2378"},"modified":"2018-06-05T07:47:46","modified_gmt":"2018-06-05T07:47:46","slug":"custom-alert-dialog-example-android-studio","status":"publish","type":"post","link":"https:\/\/abhiandroid.com\/ui\/custom-alert-dialog-example-android-studio.html","title":{"rendered":"Custom Alert Dialog With Example In Android Studio"},"content":{"rendered":"<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. The dialog appears \u00a0over the current window and display the content defined in it.<br \/>\n<img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2349\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Example-In-Android-Studio-1.png\" alt=\"Custom Alert Dialog Example In Android Studio\" width=\"165\" height=\"88\" \/><br \/>\n<span style=\"color: #008000;\"><strong>AlertDialog Vs Custom AlertDialog:<\/strong><\/span><\/p>\n<p>The <a href=\"\/ui\/alertdialog\">Alert Dialog<\/a> and Custom Alert Dialog both prompts a small window to make decision. The AlertDialog make use of the defined components or methods like setIcon, setTitle, setmessage etc\u00a0but with Custom AlertDialog we can have the dialog customised and can define the layout of dialog as required. You can read more about it in <a href=\"\/ui\/alertdialog\">alert 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<hr \/>\n<h4><strong>Custom Alert Dialog Example In Android Studio:<\/strong><\/h4>\n<p>Below is the example of Custom Alert Dialog in which the functionality of Alert Dialog is defined over button click. In this example we have used a simple button and over that 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 Custom Alert Dialog example in Android Studio.<\/p>\n<p style=\"text-align: center;\"><a class=\"download\" href=\"https:\/\/www.dropbox.com\/s\/bv5cq57rf7k6qy6\/CustomAlertDialog.zip?dl=0\" target=\"_blank\" rel=\"nofollow\">Download Code (Password: abhiandroid)<\/a><\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-full wp-image-2334\" src=\"\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Example-In-Android-Studio.png\" alt=\"Custom Alert Dialog Example In Android Studio\" width=\"271\" height=\"431\" srcset=\"https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Example-In-Android-Studio.png 271w, https:\/\/abhiandroid.com\/ui\/wp-content\/uploads\/2017\/03\/Custom-Alert-Dialog-Example-In-Android-Studio-189x300.png 189w\" sizes=\"auto, (max-width: 271px) 100vw, 271px\" \/><br \/>\n<span style=\"color: #008000;\"><strong>Step 1:<\/strong><\/span> Create a new project and name it <strong>CustomAlertDialog<\/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 create XML for button and textview. On button click custom alert dialog will appear &amp; textview is 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.customalertdialog.MainActivity\"&gt;\r\n\r\n    &lt;Button\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:id=\"@+id\/buttonShowCustomDialog\"\r\n        android:onClick=\"exit\"\r\n        android:textStyle=\"normal|bold\"\r\n        style=\"@android:style\/Widget.DeviceDefault.Button.Inset\"\r\n        android:layout_alignParentBottom=\"true\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginBottom=\"131dp\"\r\n        android:text=\"@string\/click\"\/&gt;\r\n\r\n    &lt;TextView\r\n        android:text=\"CLICK TO VIEW CUSTOM ALERT DIALOG\"\r\n        android:layout_width=\"match_parent\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:layout_above=\"@+id\/buttonShowCustomDialog\"\r\n        android:layout_centerHorizontal=\"true\"\r\n        android:layout_marginBottom=\"86dp\"\r\n        android:id=\"@+id\/textView\"\r\n        android:gravity=\"center\"\r\n        android:textStyle=\"normal|bold\"\r\n        android:textSize=\"18sp\"\r\n        tools:ignore=\"HardcodedText\" \/&gt;\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 3 :<\/strong><\/span> Now create a layout file name as custom.xml .<br \/>\nIn this file we are going to define the XML for custom dialog appearance. A textbox , imageview and \u00a0a 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:app=\"http:\/\/schemas.android.com\/apk\/res-auto\"\r\n    xmlns:tools=\"http:\/\/schemas.android.com\/tools\"\r\n    android:orientation=\"vertical\" android:layout_width=\"match_parent\"\r\n    android:layout_height=\"match_parent\"&gt;\r\n\r\n    &lt;TextView\r\n        android:text=\"@string\/alert\"\r\n        android:layout_width=\"wrap_content\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:id=\"@+id\/text\"\r\n        android:textStyle=\"normal|bold\"\r\n        android:textSize=\"18sp\"\r\n        android:layout_alignParentTop=\"true\"\r\n        android:layout_toRightOf=\"@+id\/image\"\r\n        android:layout_toEndOf=\"@+id\/image\"\r\n        android:layout_marginLeft=\"21dp\"\r\n        android:layout_marginStart=\"21dp\"\r\n        android:layout_marginTop=\"12dp\" \/&gt;\r\n\r\n    &lt;ImageButton\r\n        android:layout_width=\"35dp\"\r\n        android:layout_height=\"35dp\"\r\n        app:srcCompat=\"@drawable\/stop\"\r\n        android:id=\"@+id\/image\"\r\n        android:layout_alignParentLeft=\"true\"\r\n        android:layout_alignParentStart=\"true\"\r\n        android:layout_marginLeft=\"5dp\"\r\n        android:layout_marginTop=\"5dp\"\r\n        tools:ignore=\"ContentDescription,RtlHardcoded\" \/&gt;\r\n\r\n    &lt;Button\r\n        android:id=\"@+id\/dialogButtonOK\"\r\n        android:layout_width=\"200dp\"\r\n        android:layout_height=\"wrap_content\"\r\n        android:text=\"@string\/dismiss\"\r\n        android:layout_marginTop=\"23dp\"\r\n        android:paddingRight=\"5dp\"\r\n        android:layout_below=\"@+id\/image\"\r\n        android:layout_alignLeft=\"@+id\/image\"\r\n        android:layout_alignStart=\"@+id\/image\"\r\n        android:textSize=\"18sp\"\r\n        style=\"@style\/Widget.AppCompat.Button.Colored\"\r\n        tools:ignore=\"RtlHardcoded,RtlSymmetry\" \/&gt;\r\n\r\n&lt;\/RelativeLayout&gt;\r\n<\/pre>\n<p><span style=\"color: #008000;\"><strong>Step 4 :<\/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 code onclick is added over the button click and Dialog is used to create the custom alert dialog.<\/p>\n<pre>package com.example.customalertdialog;\r\n\r\nimport android.app.Dialog;\r\nimport android.content.Context;\r\nimport android.support.v7.app.AppCompatActivity;\r\nimport android.os.Bundle;\r\nimport android.view.View;\r\nimport android.widget.Button;\r\nimport android.widget.ImageView;\r\nimport android.widget.TextView;\r\n\r\npublic class MainActivity extends AppCompatActivity {\r\n    final Context context = this;\r\n    private Button button;\r\n\r\n    public void onCreate(Bundle savedInstanceState) {\r\n        super.onCreate(savedInstanceState);\r\n        setContentView(R.layout.activity_main);\r\n        button = (Button) findViewById(R.id.buttonShowCustomDialog);\r\n        \/\/ add button listener\r\n        button.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View arg0) {\r\n                \/\/ custom dialog\r\n                final Dialog dialog = new Dialog(context);\r\n                dialog.setContentView(R.layout.custom);\r\n                Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);\r\n                \/\/ if button is clicked, close the custom dialog\r\n                dialogButton.setOnClickListener(new View.OnClickListener() {\r\n                    @Override\r\n                    public void onClick(View v) {\r\n                        dialog.dismiss();\r\n                        Toast.makeText(getApplicationContext(),\"Dismissed..!!\",Toast.LENGTH_SHORT).show();\r\n                    }\r\n                });\r\n                dialog.show();\r\n            }\r\n        });\r\n    }\r\n}<\/pre>\n<p><strong>Output:<\/strong><\/p>\n<p>Now run the App and when you will click on the button. The custom alert dialog will appear on the screen.<\/p>\n","protected":false},"excerpt":{"rendered":"<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. The dialog appears \u00a0over the current window and display the content defined in it. AlertDialog Vs Custom AlertDialog: The Alert Dialog and &hellip; <a href=\"https:\/\/abhiandroid.com\/ui\/custom-alert-dialog-example-android-studio.html\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Custom Alert Dialog With Example In Android Studio<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[],"class_list":["post-2378","post","type-post","status-publish","format-standard","hentry","category-archieve"],"acf":[],"psp_head":"<title>Custom Alert Dialog With Example In Android Studio \u2013 Abhi Android<\/title>\r\n<meta name=\"description\" content=\"The custom dialog uses DIALOG to create custom alert in android studio. Learn it following our step by step using example in Android Studio.\" \/>\r\n<meta name=\"robots\" content=\"index,follow\" \/>\r\n<link rel=\"canonical\" href=\"https:\/\/abhiandroid.com\/ui\/custom-alert-dialog-example-android-studio.html\" \/>\r\n","_links":{"self":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2378","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/types\/post"}],"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=2378"}],"version-history":[{"count":2,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2378\/revisions"}],"predecessor-version":[{"id":2696,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/posts\/2378\/revisions\/2696"}],"wp:attachment":[{"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/media?parent=2378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/categories?post=2378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/abhiandroid.com\/ui\/wp-json\/wp\/v2\/tags?post=2378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}