Site icon C1CTech

Android AlertDialog Example

&NewLine;<p>This article is about Android <strong>AlertDialog<&sol;strong> and how to use it in android application with simple examples&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<figure class&equals;"wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class&equals;"wp-block-embed&lowbar;&lowbar;wrapper">&NewLine;<amp-youtube layout&equals;"responsive" width&equals;"1200" height&equals;"675" data-videoid&equals;"9fRUV1IJfCQ" title&equals;"Android AlertDialog Example"><a placeholder href&equals;"https&colon;&sol;&sol;youtu&period;be&sol;9fRUV1IJfCQ"><img src&equals;"https&colon;&sol;&sol;i&period;ytimg&period;com&sol;vi&sol;9fRUV1IJfCQ&sol;hqdefault&period;jpg" layout&equals;"fill" object-fit&equals;"cover" alt&equals;"Android AlertDialog Example"><&sol;a><&sol;amp-youtube>&NewLine;<&sol;div><&sol;figure>&NewLine;&NewLine;&NewLine;&NewLine;<div class&equals;"wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">&NewLine;<div class&equals;"wp-block-button is-style-fill"><a class&equals;"wp-block-button&lowbar;&lowbar;link has-white-color has-text-color has-background" href&equals;"https&colon;&sol;&sol;github&period;com&sol;arunk7839&sol;AndroidAlertDialogDemo" style&equals;"background-color&colon;&num;520599" target&equals;"&lowbar;blank" rel&equals;"noreferrer noopener"><strong>DOWNLOAD CODE<&sol;strong><&sol;a><&sol;div>&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">AlertDialog<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">AlertDialog<&sol;span><&sol;strong> is a small window that prompts the user to make a decision or enter additional information&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>A dialog does not fill the screen and is normally used for events that require users to take an action before they can proceed&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>AlertDialog With Buttons<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>In AlertDialog&comma; you can add three different action buttons&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Positive<&sol;span><&sol;strong>&colon; You should use this to accept and continue with the action &lpar;<strong> &&num;8220&semi;OK&&num;8221&semi; <&sol;strong>or &&num;8220&semi;YES&&num;8221&semi; action&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Negative<&sol;span><&sol;strong>&colon; You should use this to cancel the action&lpar; <strong>&&num;8220&semi;CANCEL&&num;8221&semi; or &&num;8220&semi;NO&&num;8221&semi; <&sol;strong>action&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">Neutral<&sol;span><&sol;strong>&colon; You should use this when the user may not want to proceed with the action&comma; but doesn&&num;8217&semi;t necessarily want to cancel &lpar; <strong>&&num;8220&semi;Remind me later&&num;8221&semi;<&sol;strong> action&rpar;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>Note&colon;<&sol;strong> <&sol;span>You can only add one of each button type to the AlertDialog&period; That is&comma; you cannot have more than one &&num;8220&semi;positive&&num;8221&semi; button&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of AlertDialog which contains title&comma; message and three buttons &lpar;Positive&comma; Negative and Neutral&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an instance of AlertDialog&period;Builder<&sol;span><&sol;strong>&NewLine;<&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><em>&sol;&sol;set dialog properties<&sol;em>&NewLine;<em>&sol;&sol;Set the title displayed in the Dialog&period;<&sol;em><&sol;span><&sol;strong><em>&NewLine;<&sol;em>builder&period;setTitle&lpar;R&period;string&period;<strong><em>alert&lowbar;dialog&lowbar;title<&sol;em><&sol;strong>&rpar;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set the icon to be used in the title&period;<&sol;span><&sol;strong><&sol;em>&NewLine; &period;setIcon&lpar;R&period;drawable&period;<strong><em>alert&lowbar;icon<&sol;em><&sol;strong>&rpar;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set the message to display in the Dialog&period;<&sol;span><&sol;strong><&sol;em>&NewLine; &period;setMessage&lpar;R&period;string&period;<strong><em>dialog&lowbar;message<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set a listener to be invoked when the positive button of the dialog is pressed&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>builder&period;setPositiveButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;ok<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; User clicked OK button<&sol;span><&sol;strong>&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"OK pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set a listener to be invoked when the negative button of the dialog is pressed&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>builder&period;setNegativeButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;cancel<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; User clicked CANCEL button<&sol;span><&sol;strong>&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"CANCEL pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set a listener to be invoked when the neutral button of the dialog is pressed&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>builder&period;setNeutralButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;got&lowbar;it<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; User clicked GOT IT button<&sol;span><&sol;strong>&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"GOT IT pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an AlertDialog<&sol;span><&sol;strong>&NewLine;<&sol;em>AlertDialog alertDialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;display the dialog on screen<&sol;span><&sol;strong>&NewLine;<&sol;em>alertDialog&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">AlertDialog With List<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>There are three kinds of lists available with the AlertDialog&nbsp&semi;APIs&colon;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<ul class&equals;"wp-block-list"><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">A single-choice list<&sol;span><&sol;strong>&colon; To add a list of single-choice items in the dialog as the content&comma; use <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setItems&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;li><li><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">A single-choice list &lpar;radio buttons&rpar;&colon;<&sol;span> <&sol;strong>To add a list of single-choice items&lpar;radio buttons&rpar; in the dialog as the content&comma; use <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setSingleChoiceItems&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;li><li><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color"><strong>A multiple-choice list &lpar;checkboxes&rpar;<&sol;strong>&colon;<&sol;span>To add a list of multiple-choice list &lpar;checkboxes&rpar; in the dialog as the content&comma; use <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setMultiChoiceItems&lpar;&rpar;<&sol;span><&sol;strong> method&period;<&sol;li><&sol;ul>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of AlertDialog which contains title and single-choice list&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an instance of AlertDialog&period;Builder<&sol;span><&sol;strong>&NewLine;<&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get string array &lpar;languages&rpar;<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>final <&sol;strong>String&lbrack;&rsqb; language &equals; getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;<strong><em>languages<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;builder&period;setTitle&lpar;getText&lpar;R&period;string&period;<strong><em>select&lowbar;language&lowbar;title<&sol;em><&sol;strong>&rpar;&rpar;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set language&lpar;list of languages&rpar; to be displayed in the dialog as the content&period;<&sol;span><&sol;strong>&NewLine; <&sol;em>&period;setItems&lpar;language&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>indexPosition&rpar; &lbrace;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol; The 'indexPosition' argument contains the index position&NewLine; &sol;&sol; of the selected item<&sol;span><&sol;strong>&NewLine; <&sol;em>String selectedItem &equals; Arrays&period;<em>asList<&sol;em>&lpar;language&rpar;&period;get&lpar;indexPosition&rpar;&semi;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Selected Language&colon; " <&sol;strong>&plus; selectedItem&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine; &rcub;&NewLine; &rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an AlertDialog<&sol;span><&sol;strong>&NewLine;<&sol;em>AlertDialog alertDialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;display the dialog on screen<&sol;span><&sol;strong>&NewLine;<&sol;em>alertDialog&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color"><strong>AlertDialog With Custom Layout<&sol;strong><&sol;span><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p>Android AlertDialog allows you to set custom layout in a dialog&period; <&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>If you want a custom layout in a dialog&comma; create a layout and add it to an <strong>AlertDialog<&sol;strong>&nbsp&semi;by calling&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">setView&lpar;&rpar;<&sol;span><&sol;strong> on your&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">AlertDialog&period;Builder<&sol;span><&sol;strong> object&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p>The below code snippet is an example of AlertDialog with custom layout&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an instance of AlertDialog&period;Builder<&sol;span><&sol;strong>&NewLine;<&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get view object represents custom layout of dialog&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>View view &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>dialog&lowbar;custom&lowbar;layout<&sol;em><&sol;strong>&comma; <strong>null<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get button and spinner by its id from view object&period;<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>final <&sol;strong>Spinner spinner &equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>spinner<&sol;em><&sol;strong>&rpar;&semi;&NewLine;Button btnOk &equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;ok<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;ArrayList&lt&semi;String&gt&semi; list &equals; <strong>new <&sol;strong>ArrayList&lt&semi;&gt&semi;&lpar;Arrays&period;<em>asList<&sol;em>&lpar;getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;<strong><em>languages<&sol;em><&sol;strong>&rpar;&rpar;&rpar;&semi;&NewLine;ArrayAdapter&lt&semi;String&gt&semi; adapter &equals; <strong>new <&sol;strong>ArrayAdapter&lt&semi;&gt&semi;&lpar;<strong>this<&sol;strong>&comma; android&period;R&period;layout&period;<strong><em>simple&lowbar;spinner&lowbar;item<&sol;em><&sol;strong>&comma; list&rpar;&semi;&NewLine;adapter&period;setDropDownViewResource&lpar;android&period;R&period;layout&period;<strong><em>simple&lowbar;spinner&lowbar;dropdown&lowbar;item<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set adapter in spinner&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>spinner&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Sets a custom view to be the contents of the alert dialog&period;<&sol;span><&sol;strong>&NewLine;<&sol;em>builder&period;setView&lpar;view&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an AlertDialog<&sol;span><&sol;strong>&NewLine;<&sol;em><strong>final <&sol;strong>AlertDialog dialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on OK button click<&sol;span><&sol;strong>&NewLine;<&sol;em>btnOk&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine; &commat;Override&NewLine; <strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine; dialog&period;dismiss&lpar;&rpar;&semi;&NewLine; String selectedItem &equals; &lpar;String&rpar; spinner&period;getSelectedItem&lpar;&rpar;&semi;&NewLine; Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Selected Language&colon; " <&sol;strong>&plus; selectedItem&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine; &rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;display the dialog on screen<&sol;span><&sol;strong>&NewLine;<&sol;em>dialog&period;show&lpar;&rpar;&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>Let&&num;8217&semi;s understand different ways of implementing an AlertDialog in android application with a simple example&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<h3 class&equals;"wp-block-heading"><strong><span style&equals;"color&colon;&num;520599" class&equals;"has-inline-color">Creating New Project<&sol;span><&sol;strong><&sol;h3>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>1<&sol;strong>&period; In&nbsp&semi;<strong>Android Studio<&sol;strong>&comma; go to&nbsp&semi;<strong>File &Implies; New Project<&sol;strong>&comma; fill all the details required to create a new project and then click on&nbsp&semi;<strong>finish<&sol;strong>&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>2<&sol;strong>&period; Open the layout file&nbsp&semi;<span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color"><strong>activity&lowbar;main&period;xml<&sol;strong>&nbsp&semi;<&sol;span>and add the below code&period; This layout file consist of three buttons&comma;&nbsp&semi;<strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">AlertDialog With Buttons<&sol;span><&sol;strong>&nbsp&semi;&lpar;show AlertDialog with buttons&rpar;&comma;&nbsp&semi;<strong><strong><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">AlertDialog With List<&sol;span><&sol;strong><&sol;strong><&sol;strong>&nbsp&semi;&lpar;show AlertDialog with list&rpar;&comma;&nbsp&semi;<strong><strong><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">AlertDialog With Custom Layout<&sol;span><&sol;strong><&sol;strong><&sol;strong>&nbsp&semi;&lpar;show AlertDialog with custom layout&rpar;&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">activity&lowbar;main&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version&equals;"1&period;0" encoding&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;&NewLine;<&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout xmlns&colon;android&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"&NewLine; xmlns&colon;app&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"&NewLine; xmlns&colon;tools&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;tools"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; tools&colon;context&equals;"&period;MainActivity"<&sol;strong>&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;buttons"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;marginLeft&equals;"&commat;dimen&sol;margin&lowbar;15dp"&NewLine; android&colon;layout&lowbar;marginTop&equals;"&commat;dimen&sol;margin&lowbar;30dp"&NewLine; android&colon;layout&lowbar;marginRight&equals;"&commat;dimen&sol;margin&lowbar;15dp"&NewLine; android&colon;layout&lowbar;marginBottom&equals;"&commat;dimen&sol;margin&lowbar;15dp"&NewLine; android&colon;text&equals;"&commat;string&sol;alert&lowbar;dialog&lowbar;with&lowbar;buttons"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;18sp"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;list"&NewLine; android&colon;textAllCaps&equals;"false"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toTopOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintVertical&lowbar;chainStyle&equals;"packed" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;list"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;margin&equals;"&commat;dimen&sol;margin&lowbar;15dp"&NewLine; android&colon;text&equals;"&commat;string&sol;alert&lowbar;dialog&lowbar;with&lowbar;list"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;18sp"&NewLine; android&colon;textAllCaps&equals;"false"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toTopOf&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;customlayout"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;buttons" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&NewLine; &lt&semi;<strong>Button&NewLine; android&colon;id&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;customlayout"&NewLine; android&colon;layout&lowbar;width&equals;"match&lowbar;parent"&NewLine; android&colon;layout&lowbar;height&equals;"wrap&lowbar;content"&NewLine; android&colon;layout&lowbar;margin&equals;"&commat;dimen&sol;margin&lowbar;15dp"&NewLine; android&colon;text&equals;"&commat;string&sol;alert&lowbar;dialog&lowbar;with&lowbar;custom&lowbar;layout"&NewLine; android&colon;textSize&equals;"&commat;dimen&sol;textsize&lowbar;18sp"&NewLine; android&colon;textAllCaps&equals;"false"&NewLine; app&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintEnd&lowbar;toEndOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintHorizontal&lowbar;bias&equals;"0&period;5"&NewLine; app&colon;layout&lowbar;constraintStart&lowbar;toStartOf&equals;"parent"&NewLine; app&colon;layout&lowbar;constraintTop&lowbar;toBottomOf&equals;"&commat;&plus;id&sol;btn&lowbar;adw&lowbar;list" <&sol;strong>&sol;&gt&semi;&NewLine;&NewLine;&NewLine;&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong>3&period;&nbsp&semi;<&sol;strong>Create a new layout file&nbsp&semi;<strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">dialog&lowbar;custom&lowbar;layout&period;xml<&sol;span><&sol;strong>&nbsp&semi;&lpar;layout-&gt&semi;New-&gt&semi;Layout Resource File&rpar; and add the below code&period; This layout file represents the custom layout of AlertDialog&period;<&sol;p>&NewLine;<&excl;-- WP QUADS Content Ad Plugin v&period; 2&period;0&period;98&period;1 -->&NewLine;<div class&equals;"quads-location quads-ad2" id&equals;"quads-ad2" style&equals;"float&colon;none&semi;margin&colon;0px&semi;">&NewLine;&NewLine;<&sol;div>&NewLine;&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">dialog&lowbar;custom&lowbar;layout&period;xml<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><em>&lt&semi;&quest;<&sol;em><strong>xml version<&sol;strong><strong>&equals;"1&period;0" <&sol;strong><strong>encoding<&sol;strong><strong>&equals;"utf-8"<&sol;strong><em>&quest;&gt&semi;<br><&sol;em>&lt&semi;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>android<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res&sol;android"<br><&sol;strong><strong> <&sol;strong><strong>xmlns&colon;<&sol;strong><strong>app<&sol;strong><strong>&equals;"http&colon;&sol;&sol;schemas&period;android&period;com&sol;apk&sol;res-auto"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<&sol;strong>&gt&semi;<br><br><br> &lt&semi;<strong>Button<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;btn&lowbar;ok"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;marginTop<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;20dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;marginBottom<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;20dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;dialog&lowbar;btn&lowbar;ok"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textColor<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;white"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textStyle<&sol;strong><strong>&equals;"bold"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textSize<&sol;strong><strong>&equals;"&commat;dimen&sol;textsize&lowbar;15sp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;background<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;dark"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintBottom&lowbar;toBottomOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;spinner" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>Spinner<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;spinner"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;margin<&sol;strong><strong>&equals;"&commat;dimen&sol;margin&lowbar;20dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;12dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;prompt<&sol;strong><strong>&equals;"&commat;string&sol;dialog&lowbar;btn&lowbar;ok"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toBottomOf<&sol;strong><strong>&equals;"&commat;&plus;id&sol;textView" <&sol;strong>&sol;&gt&semi;<br><br> &lt&semi;<strong>TextView<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;id<&sol;strong><strong>&equals;"&commat;&plus;id&sol;textView"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;width<&sol;strong><strong>&equals;"match&lowbar;parent"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;layout&lowbar;height<&sol;strong><strong>&equals;"wrap&lowbar;content"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;background<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;holo&lowbar;orange&lowbar;dark"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;padding<&sol;strong><strong>&equals;"&commat;dimen&sol;padding&lowbar;12dp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;text<&sol;strong><strong>&equals;"&commat;string&sol;dialog&lowbar;title"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textColor<&sol;strong><strong>&equals;"&commat;android&colon;color&sol;white"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textSize<&sol;strong><strong>&equals;"&commat;dimen&sol;textsize&lowbar;20sp"<br><&sol;strong><strong> <&sol;strong><strong>android<&sol;strong><strong>&colon;textStyle<&sol;strong><strong>&equals;"bold"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintEnd&lowbar;toEndOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintHorizontal&lowbar;bias<&sol;strong><strong>&equals;"0&period;5"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintStart&lowbar;toStartOf<&sol;strong><strong>&equals;"parent"<br><&sol;strong><strong> <&sol;strong><strong>app<&sol;strong><strong>&colon;layout&lowbar;constraintTop&lowbar;toTopOf<&sol;strong><strong>&equals;"parent" <&sol;strong>&sol;&gt&semi;<br><br><br>&lt&semi;&sol;<strong>androidx&period;constraintlayout&period;widget&period;ConstraintLayout<&sol;strong>&gt&semi;<&sol;pre>&NewLine;&NewLine;&NewLine;&NewLine;<p>4&period; Open <strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">MainActivity&period;java<&sol;span><&sol;strong> and add the below code&period; This activity contains three buttons with click listener to show the different ways of implementing the AlertDialog&period;<&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<p><strong><span style&equals;"color&colon;&num;0000ff" class&equals;"has-inline-color">MainActivity&period;java<&sol;span><&sol;strong><&sol;p>&NewLine;&NewLine;&NewLine;&NewLine;<pre class&equals;"wp-block-preformatted"><strong>package <&sol;strong>com&period;c1ctech&period;androidalertdialogdemo&semi;&NewLine;&NewLine;<strong>import <&sol;strong>android&period;content&period;DialogInterface&semi;&NewLine;<strong>import <&sol;strong>android&period;os&period;Bundle&semi;&NewLine;<strong>import <&sol;strong>android&period;view&period;View&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;ArrayAdapter&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Button&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Spinner&semi;&NewLine;<strong>import <&sol;strong>android&period;widget&period;Toast&semi;&NewLine;&NewLine;<strong>import <&sol;strong>java&period;util&period;ArrayList&semi;&NewLine;<strong>import <&sol;strong>java&period;util&period;Arrays&semi;&NewLine;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AlertDialog&semi;&NewLine;<strong>import <&sol;strong>androidx&period;appcompat&period;app&period;AppCompatActivity&semi;&NewLine;&NewLine;<strong>public class <&sol;strong>MainActivity <strong>extends <&sol;strong>AppCompatActivity &lbrace;&NewLine;&NewLine;Button <strong>btnAlertDialogWithButtons<&sol;strong>&comma; <strong>btnAlertDialogWithList<&sol;strong>&comma; <strong>btnAlertDialogWithCustomLayout<&sol;strong>&semi;&NewLine;&NewLine;&commat;Override&NewLine;<strong>protected void <&sol;strong>onCreate&lpar;Bundle savedInstanceState&rpar; &lbrace;&NewLine;<strong>super<&sol;strong>&period;onCreate&lpar;savedInstanceState&rpar;&semi;&NewLine;setContentView&lpar;R&period;layout&period;<strong><em>activity&lowbar;main<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;get button by its id<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnAlertDialogWithButtons <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;adw&lowbar;buttons<&sol;em><&sol;strong>&rpar;&semi;&NewLine;<strong>btnAlertDialogWithList <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;adw&lowbar;list<&sol;em><&sol;strong>&rpar;&semi;&NewLine;<strong>btnAlertDialogWithCustomLayout <&sol;strong>&equals; findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;adw&lowbar;customlayout<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on btnAlertDialogWithMessage button<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnAlertDialogWithButtons<&sol;strong>&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;createAlertDialogWithButtons&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on btnAlertDialogWithList button<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnAlertDialogWithList<&sol;strong>&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;createAlertDialogWithList&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set click listener on btnAlertDialogWithCustomLayout button<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>btnAlertDialogWithCustomLayout<&sol;strong>&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;createAlertDialogWithCustomLayout&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create and show alert dialog with title&comma; message&comma; buttons&lpar;positive&comma;negative and neutral&rpar;&period;<&sol;span><&sol;strong>&NewLine; <&sol;em><strong>void <&sol;strong>createAlertDialogWithButtons&lpar;&rpar; &lbrace;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;create an instance of AlertDialog&period;Builder<&sol;span><&sol;strong>&NewLine; <&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine; <em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;set dialog properties&NewLine; &sol;&sol;Set the title displayed in the Dialog&period;<&sol;span><&sol;strong>&NewLine; <&sol;em>builder&period;setTitle&lpar;R&period;string&period;<strong><em>alert&lowbar;dialog&lowbar;title<&sol;em><&sol;strong>&rpar;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set the icon to be used in the title&period;<&sol;span><&sol;strong>&NewLine; <&sol;em>&period;setIcon&lpar;R&period;drawable&period;<strong><em>alert&lowbar;icon<&sol;em><&sol;strong>&rpar;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set the message to display in the Dialog&period;<&sol;span><&sol;strong>&NewLine; <&sol;em>&period;setMessage&lpar;R&period;string&period;<strong><em>dialog&lowbar;message<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em><strong><span style&equals;"color&colon;&num;04603e" class&equals;"has-inline-color">&sol;&sol;Set a listener to be invoked when the positive button of the dialog is pressed&period;<&sol;span><&sol;strong>&NewLine; <&sol;em>builder&period;setPositiveButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;ok<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine;<em>&sol;&sol; User clicked OK button&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine;Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"OK pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;Set a listener to be invoked when the negative button of the dialog is pressed&period;&NewLine; <&sol;em>builder&period;setNegativeButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;cancel<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine;<em>&sol;&sol; User clicked CANCEL button&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine;Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"CANCEL pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;Set a listener to be invoked when the neutral button of the dialog is pressed&period;&NewLine; <&sol;em>builder&period;setNeutralButton&lpar;R&period;string&period;<strong><em>dialog&lowbar;btn&lowbar;got&lowbar;it<&sol;em><&sol;strong>&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>i&rpar; &lbrace;&NewLine;<em>&sol;&sol; User clicked GOT IT button&NewLine; <&sol;em>dialogInterface&period;dismiss&lpar;&rpar;&semi;&NewLine;Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"GOT IT pressed "<&sol;strong>&comma; Toast&period;<strong><em>LENGTH&lowbar;SHORT<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;create an AlertDialog&NewLine; <&sol;em>AlertDialog alertDialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;display the dialog on screen&NewLine; <&sol;em>alertDialog&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&NewLine;&rcub;&NewLine;&NewLine;<em>&sol;&sol;create and show alert dialog with title and single-choice list&period;&NewLine; <&sol;em><strong>void <&sol;strong>createAlertDialogWithList&lpar;&rpar; &lbrace;&NewLine;&NewLine;<em>&sol;&sol;create an instance of AlertDialog&period;Builder&NewLine; <&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;get string array &lpar;languages&rpar;&NewLine; <&sol;em><strong>final <&sol;strong>String&lbrack;&rsqb; language &equals; getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;<strong><em>languages<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;builder&period;setTitle&lpar;getText&lpar;R&period;string&period;<strong><em>select&lowbar;language&lowbar;title<&sol;em><&sol;strong>&rpar;&rpar;&NewLine;&NewLine;<em>&sol;&sol;Set language&lpar;list of languages&rpar; to be displayed in the dialog as the content&period;&NewLine; <&sol;em>&period;setItems&lpar;language&comma; <strong>new <&sol;strong>DialogInterface&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;DialogInterface dialogInterface&comma; <strong>int <&sol;strong>indexPosition&rpar; &lbrace;&NewLine;&NewLine;<em>&sol;&sol; The 'indexPosition' argument contains the index position&NewLine; &sol;&sol; of the selected item&NewLine; <&sol;em>String selectedItem &equals; Arrays&period;<em>asList<&sol;em>&lpar;language&rpar;&period;get&lpar;indexPosition&rpar;&semi;&NewLine;Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Selected Language&colon; " <&sol;strong>&plus; selectedItem&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;create an AlertDialog&NewLine; <&sol;em>AlertDialog alertDialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;display the dialog on screen&NewLine; <&sol;em>alertDialog&period;show&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&NewLine;<em>&sol;&sol;create and show alert dialog with custom layout&period;&NewLine; <&sol;em><strong>void <&sol;strong>createAlertDialogWithCustomLayout&lpar;&rpar; &lbrace;&NewLine;&NewLine;<em>&sol;&sol;create an instance of AlertDialog&period;Builder&NewLine; <&sol;em>AlertDialog&period;Builder builder &equals; <strong>new <&sol;strong>AlertDialog&period;Builder&lpar;<strong>this<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;get view object represents custom layout of dialog&period;&NewLine; <&sol;em>View view &equals; getLayoutInflater&lpar;&rpar;&period;inflate&lpar;R&period;layout&period;<strong><em>dialog&lowbar;custom&lowbar;layout<&sol;em><&sol;strong>&comma; <strong>null<&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;get button and spinner by its id from view object&period;&NewLine; <&sol;em><strong>final <&sol;strong>Spinner spinner &equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>spinner<&sol;em><&sol;strong>&rpar;&semi;&NewLine;Button btnOk &equals; view&period;findViewById&lpar;R&period;id&period;<strong><em>btn&lowbar;ok<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;ArrayList&lt&semi;String&gt&semi; list &equals; <strong>new <&sol;strong>ArrayList&lt&semi;&gt&semi;&lpar;Arrays&period;<em>asList<&sol;em>&lpar;getResources&lpar;&rpar;&period;getStringArray&lpar;R&period;array&period;<strong><em>languages<&sol;em><&sol;strong>&rpar;&rpar;&rpar;&semi;&NewLine;ArrayAdapter&lt&semi;String&gt&semi; adapter &equals; <strong>new <&sol;strong>ArrayAdapter&lt&semi;&gt&semi;&lpar;<strong>this<&sol;strong>&comma; android&period;R&period;layout&period;<strong><em>simple&lowbar;spinner&lowbar;item<&sol;em><&sol;strong>&comma; list&rpar;&semi;&NewLine;adapter&period;setDropDownViewResource&lpar;android&period;R&period;layout&period;<strong><em>simple&lowbar;spinner&lowbar;dropdown&lowbar;item<&sol;em><&sol;strong>&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;set adapter in spinner&period;&NewLine; <&sol;em>spinner&period;setAdapter&lpar;adapter&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;Sets a custom view to be the contents of the alert dialog&period;&NewLine; <&sol;em>builder&period;setView&lpar;view&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;create an AlertDialog&NewLine; <&sol;em><strong>final <&sol;strong>AlertDialog dialog &equals; builder&period;create&lpar;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;set click listener on OK button click&NewLine; <&sol;em>btnOk&period;setOnClickListener&lpar;<strong>new <&sol;strong>View&period;OnClickListener&lpar;&rpar; &lbrace;&NewLine;&commat;Override&NewLine;<strong>public void <&sol;strong>onClick&lpar;View view&rpar; &lbrace;&NewLine;dialog&period;dismiss&lpar;&rpar;&semi;&NewLine;String selectedItem &equals; &lpar;String&rpar; spinner&period;getSelectedItem&lpar;&rpar;&semi;&NewLine;Toast&period;<em>makeText<&sol;em>&lpar;getApplicationContext&lpar;&rpar;&comma; <strong>"Selected Language&colon; " <&sol;strong>&plus; selectedItem&comma; Toast&period;<strong><em>LENGTH&lowbar;LONG<&sol;em><&sol;strong>&rpar;&period;show&lpar;&rpar;&semi;&NewLine;&rcub;&NewLine;&rcub;&rpar;&semi;&NewLine;&NewLine;<em>&sol;&sol;display the dialog on screen&NewLine; <&sol;em>dialog&period;show&lpar;&rpar;&semi;&NewLine;&NewLine;&rcub;&NewLine;&rcub;<&sol;pre>&NewLine;&NewLine;

Exit mobile version