{"id":2610,"date":"2016-04-18T10:07:46","date_gmt":"2016-04-18T10:07:46","guid":{"rendered":"http:\/\/truelogic.org\/wordpress\/?p=2610"},"modified":"2016-04-18T10:07:46","modified_gmt":"2016-04-18T10:07:46","slug":"create-a-generic-yes-no-dialog-in-android","status":"publish","type":"post","link":"https:\/\/truelogic.org\/wordpress\/2016\/04\/18\/create-a-generic-yes-no-dialog-in-android\/","title":{"rendered":"Create A Generic Yes-No Dialog in Android"},"content":{"rendered":"            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushXml.js\"><\/script>\n            <script type=\"text\/javascript\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/plugins\/wordpress-code-snippet\/scripts\/shBrushJava.js\"><\/script>\n<p>Often in apps, we need to popup a dialog that will show a message and take a Yes or No in response from the user. Rather than make different dialogs for various situations, it is better to create a generic dialog which can be used in multiple situations.<\/p>\n<p>Below we create a layout xml for a dialog and then we call a Dialog object in code, within any Activity, where we need to get a Yes\/No response from the user.<\/p>\n<p>&nbsp;<\/p>\n<h4>Dialog Layout<\/h4>\n<p><pre class=\"brush: xml\">&lt;?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?&gt;\r\n&lt;RelativeLayout xmlns:android=&quot;http:\/\/schemas.android.com\/apk\/res\/android&quot;\r\n    xmlns:tools=&quot;http:\/\/schemas.android.com\/tools&quot;\r\n    android:layout_width=&quot;match_parent&quot;\r\n    android:layout_height=&quot;match_parent&quot;\r\n    android:paddingBottom=&quot;@dimen\/activity_vertical_margin&quot;\r\n    android:paddingLeft=&quot;@dimen\/activity_horizontal_margin&quot;\r\n    android:paddingRight=&quot;@dimen\/activity_horizontal_margin&quot;\r\n    android:paddingTop=&quot;@dimen\/activity_vertical_margin&quot;\r\n\r\n    &gt;\r\n\r\n\r\n    &lt;LinearLayout\r\n        android:orientation=&quot;vertical&quot;\r\n        android:layout_width=&quot;match_parent&quot;\r\n        android:layout_height=&quot;wrap_content&quot;\r\n        android:layout_centerVertical=&quot;true&quot;\r\n        android:layout_centerHorizontal=&quot;true&quot;\r\n        android:paddingTop=&quot;20dp&quot;\r\n        android:paddingBottom=&quot;20dp&quot;\r\n        android:paddingLeft=&quot;10dp&quot;\r\n        android:paddingRight=&quot;10dp&quot;\r\n        android:id=&quot;@+id\/linearLayout&quot;&gt;\r\n\r\n        &lt;LinearLayout\r\n            android:layout_width=&quot;fill_parent&quot;\r\n            android:layout_height=&quot;wrap_content&quot;\r\n            android:orientation=&quot;horizontal&quot;\r\n            android:layout_marginBottom=&quot;10dp&quot;&gt;\r\n\r\n            &lt;TextView\r\n                android:layout_width=&quot;0dp&quot;\r\n                android:layout_height=&quot;wrap_content&quot;\r\n                android:layout_weight=&quot;1.0&quot;\r\n                android:textAlignment=&quot;center&quot;\r\n                android:id=&quot;@+id\/textDialogYesNoMessage&quot;\r\n                android:text=&quot;Dialog message will come here&quot;\r\n                \/&gt;\r\n\r\n\r\n        &lt;\/LinearLayout&gt;\r\n\r\n\r\n\r\n\r\n        &lt;LinearLayout\r\n            android:layout_width=&quot;fill_parent&quot;\r\n            android:layout_height=&quot;wrap_content&quot;\r\n            android:orientation=&quot;horizontal&quot;\r\n            android:layout_marginTop=&quot;10dp&quot;&gt;\r\n\r\n            &lt;Button\r\n                style=&quot;?android:attr\/buttonStyleSmall&quot;\r\n                android:layout_width=&quot;wrap_content&quot;\r\n                android:layout_height=&quot;wrap_content&quot;\r\n                android:text=&quot;Yes&quot;\r\n                android:id=&quot;@+id\/btnDialogYes&quot;\r\n                android:textColor=&quot;@color\/white&quot;\r\n                android:layout_weight=&quot;0.20&quot;\r\n                \/&gt;\r\n            &lt;View\r\n                android:layout_width=&quot;0dp&quot;\r\n                android:layout_height=&quot;0dp&quot;\r\n                android:layout_weight=&quot;0.45&quot; \/&gt;\r\n            &lt;Button\r\n                style=&quot;?android:attr\/buttonStyleSmall&quot;\r\n                android:layout_width=&quot;wrap_content&quot;\r\n                android:layout_height=&quot;wrap_content&quot;\r\n                android:text=&quot;No&quot;\r\n                android:id=&quot;@+id\/btnDialogNo&quot;\r\n                android:textColor=&quot;@color\/white&quot;\r\n                android:layout_weight=&quot;0.20&quot;\r\n                       \/&gt;\r\n\r\n\r\n        &lt;\/LinearLayout&gt;\r\n\r\n    &lt;\/LinearLayout&gt;\r\n\r\n\r\n\r\n&lt;\/RelativeLayout&gt;<\/pre><\/p>\n<p>In this dialog display, the TextView can be changed to show whatever text we want to show.\u00a0 We can add listeners to the Yes and No buttons and then take appropriate action. Below is an example case where a Payment entry has to be deleted. Likewise, for other situations, you can customize what you want to show in the Dialog and how you want to handle the Dialog buttons.<\/p>\n<p>This is\u00a0 a basic Dialog with just a TextView and two Buttons. You can add more components to the layout as required and handle them in code as required.<\/p>\n<h4>Calling Dialog in Code<\/h4>\n<p><pre class=\"brush: java\">   public void deletePayment(int id) {\r\n\r\n        \/\/ custom dialog\r\n        final Dialog dialog = new Dialog(this);\r\n        dialog.setContentView(R.layout.dialog_yesno);\r\n        dialog.setTitle(&quot;Delete Payment&quot;);\r\n\r\n        \/\/ set the custom dialog components -\r\n        TextView text = (TextView) dialog.findViewById(R.id.textDialogYesNoMessage);\r\n        text.setText(&quot;This will delete payment for Invoice #212&quot;);\r\n\r\n\r\n        Button btnYes = (Button) dialog.findViewById(R.id.btnDialogYes);\r\n        \/\/ if button is clicked, close the custom dialog\r\n        btnYes.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                dialog.dismiss();\r\n               \/\/ do some process here to handle deletion\r\n            }\r\n        });\r\n        Button btnNo = (Button) dialog.findViewById(R.id.btnDialogNo);\r\n        btnNo.setOnClickListener(new View.OnClickListener() {\r\n            @Override\r\n            public void onClick(View v) {\r\n                dialog.dismiss();\r\n            }\r\n        });\r\n\r\n        dialog.show();\r\n    }<\/pre><\/p>\n<h4>Screenshots<\/h4>\n<p>This is how the dialog layout looks in Android Studio.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2615\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2016\/04\/Screenshot-from-2016-04-18-151754.png\" alt=\"Screenshot from 2016-04-18 15:17:54\" width=\"223\" height=\"410\" \/><\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>&nbsp;<\/p>\n<p>This is how the Delete Payment dialog looks.<img loading=\"lazy\" decoding=\"async\" class=\"alignleft size-full wp-image-2616\" src=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2016\/04\/Screenshot_2016-04-18-15-29-10.png\" alt=\"Screenshot_2016-04-18-15-29-10\" width=\"480\" height=\"854\" srcset=\"https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2016\/04\/Screenshot_2016-04-18-15-29-10.png 480w, https:\/\/truelogic.org\/wordpress\/wp-content\/uploads\/2016\/04\/Screenshot_2016-04-18-15-29-10-300x534.png 300w\" sizes=\"auto, (max-width: 480px) 100vw, 480px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>Often in apps, we need to popup a dialog that will show a message and take a Yes or No in response from the user. <a class=\"mh-excerpt-more\" href=\"https:\/\/truelogic.org\/wordpress\/2016\/04\/18\/create-a-generic-yes-no-dialog-in-android\/\" title=\"Create A Generic Yes-No Dialog in Android\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":2618,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[295],"tags":[],"class_list":["post-2610","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android-dev"],"_links":{"self":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2610","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/comments?post=2610"}],"version-history":[{"count":4,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2610\/revisions"}],"predecessor-version":[{"id":2617,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/posts\/2610\/revisions\/2617"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media\/2618"}],"wp:attachment":[{"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/media?parent=2610"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/categories?post=2610"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/truelogic.org\/wordpress\/wp-json\/wp\/v2\/tags?post=2610"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}