<p>This article is about Android <strong>AlertDialog</strong> and how to use it in android application with simple examples.</p>



<figure class="wp-block-embed-youtube wp-block-embed is-type-video is-provider-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<amp-youtube layout="responsive" width="1200" height="675" data-videoid="9fRUV1IJfCQ" title="Android AlertDialog Example"><a placeholder href="https://youtu.be/9fRUV1IJfCQ"><img src="https://i.ytimg.com/vi/9fRUV1IJfCQ/hqdefault.jpg" layout="fill" object-fit="cover" alt="Android AlertDialog Example"></a></amp-youtube>
</div></figure>



<div class="wp-block-buttons aligncenter is-layout-flex wp-block-buttons-is-layout-flex">
<div class="wp-block-button is-style-fill"><a class="wp-block-button__link has-white-color has-text-color has-background" href="https://github.com/arunk7839/AndroidAlertDialogDemo" style="background-color:#520599" target="_blank" rel="noreferrer noopener"><strong>DOWNLOAD CODE</strong></a></div>
</div>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">AlertDialog</span></strong></h3>



<p><strong><span style="color:#04603e" class="has-inline-color">AlertDialog</span></strong> is a small window that prompts the user to make a decision or enter additional information. </p>



<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.</p>



<h3 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>AlertDialog With Buttons</strong></span></h3>



<p>In AlertDialog, you can add three different action buttons:</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">Positive</span></strong>: You should use this to accept and continue with the action (<strong> &#8220;OK&#8221; </strong>or &#8220;YES&#8221; action).</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">Negative</span></strong>: You should use this to cancel the action( <strong>&#8220;CANCEL&#8221; or &#8220;NO&#8221; </strong>action).</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">Neutral</span></strong>: You should use this when the user may not want to proceed with the action, but doesn&#8217;t necessarily want to cancel ( <strong>&#8220;Remind me later&#8221;</strong> action)</p>



<p><span style="color:#520599" class="has-inline-color"><strong>Note:</strong> </span>You can only add one of each button type to the AlertDialog. That is, you cannot have more than one &#8220;positive&#8221; button.</p>



<p>The below code snippet is an example of AlertDialog which contains title, message and three buttons (Positive, Negative and Neutral).</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//create an instance of AlertDialog.Builder</span></strong>
</em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

<strong><span style="color:#04603e" class="has-inline-color"><em>//set dialog properties</em>
<em>//Set the title displayed in the Dialog.</em></span></strong><em>
</em>builder.setTitle(R.string.<strong><em>alert_dialog_title</em></strong>)
 <em><strong><span style="color:#04603e" class="has-inline-color">//Set the icon to be used in the title.</span></strong></em>
 .setIcon(R.drawable.<strong><em>alert_icon</em></strong>)
 <em><strong><span style="color:#04603e" class="has-inline-color">//Set the message to display in the Dialog.</span></strong></em>
 .setMessage(R.string.<strong><em>dialog_message</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//Set a listener to be invoked when the positive button of the dialog is pressed.</span></strong>
</em>builder.setPositiveButton(R.string.<strong><em>dialog_btn_ok</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
 <em><strong><span style="color:#04603e" class="has-inline-color">// User clicked OK button</span></strong>
 </em>dialogInterface.dismiss();
 Toast.<em>makeText</em>(getApplicationContext(), <strong>"OK pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();
 }
});

<em><strong><span style="color:#04603e" class="has-inline-color">//Set a listener to be invoked when the negative button of the dialog is pressed.</span></strong>
</em>builder.setNegativeButton(R.string.<strong><em>dialog_btn_cancel</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
 <em><strong><span style="color:#04603e" class="has-inline-color">// User clicked CANCEL button</span></strong>
 </em>dialogInterface.dismiss();
 Toast.<em>makeText</em>(getApplicationContext(), <strong>"CANCEL pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();

 }
});

<em><strong><span style="color:#04603e" class="has-inline-color">//Set a listener to be invoked when the neutral button of the dialog is pressed.</span></strong>
</em>builder.setNeutralButton(R.string.<strong><em>dialog_btn_got_it</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
 <em><strong><span style="color:#04603e" class="has-inline-color">// User clicked GOT IT button</span></strong>
 </em>dialogInterface.dismiss();
 Toast.<em>makeText</em>(getApplicationContext(), <strong>"GOT IT pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();

 }
});

<em><strong><span style="color:#04603e" class="has-inline-color">//create an AlertDialog</span></strong>
</em>AlertDialog alertDialog = builder.create();

<em><strong><span style="color:#04603e" class="has-inline-color">//display the dialog on screen</span></strong>
</em>alertDialog.show();</pre>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">AlertDialog With List</span></strong></h3>



<p>There are three kinds of lists available with the AlertDialog ;APIs:</p>



<ul class="wp-block-list"><li><strong><span style="color:#0000ff" class="has-inline-color">A single-choice list</span></strong>: To add a list of single-choice items in the dialog as the content, use <strong><span style="color:#04603e" class="has-inline-color">setItems()</span></strong> method.</li><li><strong><span style="color:#0000ff" class="has-inline-color">A single-choice list (radio buttons):</span> </strong>To add a list of single-choice items(radio buttons) in the dialog as the content, use <strong><span style="color:#04603e" class="has-inline-color">setSingleChoiceItems()</span></strong> method.</li><li><span style="color:#0000ff" class="has-inline-color"><strong>A multiple-choice list (checkboxes)</strong>:</span>To add a list of multiple-choice list (checkboxes) in the dialog as the content, use <strong><span style="color:#04603e" class="has-inline-color">setMultiChoiceItems()</span></strong> method.</li></ul>



<p>The below code snippet is an example of AlertDialog which contains title and single-choice list.</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//create an instance of AlertDialog.Builder</span></strong>
</em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//get string array (languages)</span></strong>
</em><strong>final </strong>String[] language = getResources().getStringArray(R.array.<strong><em>languages</em></strong>);

builder.setTitle(getText(R.string.<strong><em>select_language_title</em></strong>))

 <em><strong><span style="color:#04603e" class="has-inline-color">//Set language(list of languages) to be displayed in the dialog as the content.</span></strong>
 </em>.setItems(language, <strong>new </strong>DialogInterface.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>indexPosition) {

 <em><strong><span style="color:#04603e" class="has-inline-color">// The 'indexPosition' argument contains the index position
 // of the selected item</span></strong>
 </em>String selectedItem = Arrays.<em>asList</em>(language).get(indexPosition);
 Toast.<em>makeText</em>(getApplicationContext(), <strong>"Selected Language: " </strong>+ selectedItem, Toast.<strong><em>LENGTH_LONG</em></strong>).show();

 }
 });

<em><strong><span style="color:#04603e" class="has-inline-color">//create an AlertDialog</span></strong>
</em>AlertDialog alertDialog = builder.create();

<em><strong><span style="color:#04603e" class="has-inline-color">//display the dialog on screen</span></strong>
</em>alertDialog.show();</pre>



<h3 class="wp-block-heading"><span style="color:#520599" class="has-inline-color"><strong>AlertDialog With Custom Layout</strong></span></h3>



<p>Android AlertDialog allows you to set custom layout in a dialog. </p>



<p>If you want a custom layout in a dialog, create a layout and add it to an <strong>AlertDialog</strong> ;by calling ;<strong><span style="color:#04603e" class="has-inline-color">setView()</span></strong> on your ;<strong><span style="color:#04603e" class="has-inline-color">AlertDialog.Builder</span></strong> object.</p>



<p>The below code snippet is an example of AlertDialog with custom layout.</p>



<pre class="wp-block-preformatted"><em><strong><span style="color:#04603e" class="has-inline-color">//create an instance of AlertDialog.Builder</span></strong>
</em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//get view object represents custom layout of dialog.</span></strong>
</em>View view = getLayoutInflater().inflate(R.layout.<strong><em>dialog_custom_layout</em></strong>, <strong>null</strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//get button and spinner by its id from view object.</span></strong>
</em><strong>final </strong>Spinner spinner = view.findViewById(R.id.<strong><em>spinner</em></strong>);
Button btnOk = view.findViewById(R.id.<strong><em>btn_ok</em></strong>);

ArrayList<;String>; list = <strong>new </strong>ArrayList<;>;(Arrays.<em>asList</em>(getResources().getStringArray(R.array.<strong><em>languages</em></strong>)));
ArrayAdapter<;String>; adapter = <strong>new </strong>ArrayAdapter<;>;(<strong>this</strong>, android.R.layout.<strong><em>simple_spinner_item</em></strong>, list);
adapter.setDropDownViewResource(android.R.layout.<strong><em>simple_spinner_dropdown_item</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//set adapter in spinner.</span></strong>
</em>spinner.setAdapter(adapter);

<em><strong><span style="color:#04603e" class="has-inline-color">//Sets a custom view to be the contents of the alert dialog.</span></strong>
</em>builder.setView(view);

<em><strong><span style="color:#04603e" class="has-inline-color">//create an AlertDialog</span></strong>
</em><strong>final </strong>AlertDialog dialog = builder.create();

<em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on OK button click</span></strong>
</em>btnOk.setOnClickListener(<strong>new </strong>View.OnClickListener() {
 @Override
 <strong>public void </strong>onClick(View view) {
 dialog.dismiss();
 String selectedItem = (String) spinner.getSelectedItem();
 Toast.<em>makeText</em>(getApplicationContext(), <strong>"Selected Language: " </strong>+ selectedItem, Toast.<strong><em>LENGTH_LONG</em></strong>).show();
 }
});

<em><strong><span style="color:#04603e" class="has-inline-color">//display the dialog on screen</span></strong>
</em>dialog.show();</pre>



<p>Let&#8217;s understand different ways of implementing an AlertDialog in android application with a simple example.</p>



<h3 class="wp-block-heading"><strong><span style="color:#520599" class="has-inline-color">Creating New Project</span></strong></h3>



<p><strong>1</strong>. In ;<strong>Android Studio</strong>, go to ;<strong>File ⇒ New Project</strong>, fill all the details required to create a new project and then click on ;<strong>finish</strong>.</p>



<p><strong>2</strong>. Open the layout file ;<span style="color:#04603e" class="has-inline-color"><strong>activity_main.xml</strong> ;</span>and add the below code. This layout file consist of three buttons, ;<strong><span style="color:#0000ff" class="has-inline-color">AlertDialog With Buttons</span></strong> ;(show AlertDialog with buttons), ;<strong><strong><strong><span style="color:#0000ff" class="has-inline-color">AlertDialog With List</span></strong></strong></strong> ;(show AlertDialog with list), ;<strong><strong><strong><span style="color:#0000ff" class="has-inline-color">AlertDialog With Custom Layout</span></strong></strong></strong> ;(show AlertDialog with custom layout).</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">activity_main.xml</span></strong></p>



<pre class="wp-block-preformatted"><em><;?</em><strong>xml version="1.0" encoding="utf-8"</strong><em>?>;
</em><;<strong>androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:app="http://schemas.android.com/apk/res-auto"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 tools:context=".MainActivity"</strong>>;

 <;<strong>Button
 android:id="@+id/btn_adw_buttons"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="@dimen/margin_15dp"
 android:layout_marginTop="@dimen/margin_30dp"
 android:layout_marginRight="@dimen/margin_15dp"
 android:layout_marginBottom="@dimen/margin_15dp"
 android:text="@string/alert_dialog_with_buttons"
 android:textSize="@dimen/textsize_18sp"
 app:layout_constraintBottom_toTopOf="@+id/btn_adw_list"
 android:textAllCaps="false"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toTopOf="parent"
 app:layout_constraintVertical_chainStyle="packed" </strong>/>;

 <;<strong>Button
 android:id="@+id/btn_adw_list"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="@dimen/margin_15dp"
 android:text="@string/alert_dialog_with_list"
 android:textSize="@dimen/textsize_18sp"
 android:textAllCaps="false"
 app:layout_constraintBottom_toTopOf="@+id/btn_adw_customlayout"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/btn_adw_buttons" </strong>/>;


 <;<strong>Button
 android:id="@+id/btn_adw_customlayout"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_margin="@dimen/margin_15dp"
 android:text="@string/alert_dialog_with_custom_layout"
 android:textSize="@dimen/textsize_18sp"
 android:textAllCaps="false"
 app:layout_constraintBottom_toBottomOf="parent"
 app:layout_constraintEnd_toEndOf="parent"
 app:layout_constraintHorizontal_bias="0.5"
 app:layout_constraintStart_toStartOf="parent"
 app:layout_constraintTop_toBottomOf="@+id/btn_adw_list" </strong>/>;


<;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p><strong>3. ;</strong>Create a new layout file ;<strong><span style="color:#04603e" class="has-inline-color">dialog_custom_layout.xml</span></strong> ;(layout->;New->;Layout Resource File) and add the below code. This layout file represents the custom layout of AlertDialog.</p>
<!-- WP QUADS Content Ad Plugin v. 2.0.98.1 -->
<div class="quads-location quads-ad2" id="quads-ad2" style="float:none;margin:0px;">

</div>




<p><strong><span style="color:#0000ff" class="has-inline-color">dialog_custom_layout.xml</span></strong></p>



<pre class="wp-block-preformatted"><em><;?</em><strong>xml version</strong><strong>="1.0" </strong><strong>encoding</strong><strong>="utf-8"</strong><em>?>;<br></em><;<strong>androidx.constraintlayout.widget.ConstraintLayout </strong><strong>xmlns:</strong><strong>android</strong><strong>="http://schemas.android.com/apk/res/android"<br></strong><strong> </strong><strong>xmlns:</strong><strong>app</strong><strong>="http://schemas.android.com/apk/res-auto"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"</strong>>;<br><br><br> <;<strong>Button<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/btn_ok"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_marginTop</strong><strong>="@dimen/margin_20dp"<br></strong><strong> </strong><strong>android</strong><strong>:layout_marginBottom</strong><strong>="@dimen/margin_20dp"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/dialog_btn_ok"<br></strong><strong> </strong><strong>android</strong><strong>:textColor</strong><strong>="@android:color/white"<br></strong><strong> </strong><strong>android</strong><strong>:textStyle</strong><strong>="bold"<br></strong><strong> </strong><strong>android</strong><strong>:textSize</strong><strong>="@dimen/textsize_15sp"<br></strong><strong> </strong><strong>android</strong><strong>:background</strong><strong>="@android:color/holo_orange_dark"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintBottom_toBottomOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toBottomOf</strong><strong>="@+id/spinner" </strong>/>;<br><br> <;<strong>Spinner<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/spinner"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:layout_margin</strong><strong>="@dimen/margin_20dp"<br></strong><strong> </strong><strong>android</strong><strong>:padding</strong><strong>="@dimen/padding_12dp"<br></strong><strong> </strong><strong>android</strong><strong>:prompt</strong><strong>="@string/dialog_btn_ok"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toBottomOf</strong><strong>="@+id/textView" </strong>/>;<br><br> <;<strong>TextView<br></strong><strong> </strong><strong>android</strong><strong>:id</strong><strong>="@+id/textView"<br></strong><strong> </strong><strong>android</strong><strong>:layout_width</strong><strong>="match_parent"<br></strong><strong> </strong><strong>android</strong><strong>:layout_height</strong><strong>="wrap_content"<br></strong><strong> </strong><strong>android</strong><strong>:background</strong><strong>="@android:color/holo_orange_dark"<br></strong><strong> </strong><strong>android</strong><strong>:padding</strong><strong>="@dimen/padding_12dp"<br></strong><strong> </strong><strong>android</strong><strong>:text</strong><strong>="@string/dialog_title"<br></strong><strong> </strong><strong>android</strong><strong>:textColor</strong><strong>="@android:color/white"<br></strong><strong> </strong><strong>android</strong><strong>:textSize</strong><strong>="@dimen/textsize_20sp"<br></strong><strong> </strong><strong>android</strong><strong>:textStyle</strong><strong>="bold"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintEnd_toEndOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintHorizontal_bias</strong><strong>="0.5"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintStart_toStartOf</strong><strong>="parent"<br></strong><strong> </strong><strong>app</strong><strong>:layout_constraintTop_toTopOf</strong><strong>="parent" </strong>/>;<br><br><br><;/<strong>androidx.constraintlayout.widget.ConstraintLayout</strong>>;</pre>



<p>4. Open <strong><span style="color:#04603e" class="has-inline-color">MainActivity.java</span></strong> and add the below code. This activity contains three buttons with click listener to show the different ways of implementing the AlertDialog.</p>



<p><strong><span style="color:#0000ff" class="has-inline-color">MainActivity.java</span></strong></p>



<pre class="wp-block-preformatted"><strong>package </strong>com.c1ctech.androidalertdialogdemo;

<strong>import </strong>android.content.DialogInterface;
<strong>import </strong>android.os.Bundle;
<strong>import </strong>android.view.View;
<strong>import </strong>android.widget.ArrayAdapter;
<strong>import </strong>android.widget.Button;
<strong>import </strong>android.widget.Spinner;
<strong>import </strong>android.widget.Toast;

<strong>import </strong>java.util.ArrayList;
<strong>import </strong>java.util.Arrays;

<strong>import </strong>androidx.appcompat.app.AlertDialog;
<strong>import </strong>androidx.appcompat.app.AppCompatActivity;

<strong>public class </strong>MainActivity <strong>extends </strong>AppCompatActivity {

Button <strong>btnAlertDialogWithButtons</strong>, <strong>btnAlertDialogWithList</strong>, <strong>btnAlertDialogWithCustomLayout</strong>;

@Override
<strong>protected void </strong>onCreate(Bundle savedInstanceState) {
<strong>super</strong>.onCreate(savedInstanceState);
setContentView(R.layout.<strong><em>activity_main</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//get button by its id</span></strong>
 </em><strong>btnAlertDialogWithButtons </strong>= findViewById(R.id.<strong><em>btn_adw_buttons</em></strong>);
<strong>btnAlertDialogWithList </strong>= findViewById(R.id.<strong><em>btn_adw_list</em></strong>);
<strong>btnAlertDialogWithCustomLayout </strong>= findViewById(R.id.<strong><em>btn_adw_customlayout</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on btnAlertDialogWithMessage button</span></strong>
 </em><strong>btnAlertDialogWithButtons</strong>.setOnClickListener(<strong>new </strong>View.OnClickListener() {
@Override
<strong>public void </strong>onClick(View view) {
createAlertDialogWithButtons();
}
});

<em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on btnAlertDialogWithList button</span></strong>
 </em><strong>btnAlertDialogWithList</strong>.setOnClickListener(<strong>new </strong>View.OnClickListener() {
@Override
<strong>public void </strong>onClick(View view) {
createAlertDialogWithList();
}
});

<em><strong><span style="color:#04603e" class="has-inline-color">//set click listener on btnAlertDialogWithCustomLayout button</span></strong>
 </em><strong>btnAlertDialogWithCustomLayout</strong>.setOnClickListener(<strong>new </strong>View.OnClickListener() {
@Override
<strong>public void </strong>onClick(View view) {
createAlertDialogWithCustomLayout();
}
});

}

<em><strong><span style="color:#04603e" class="has-inline-color">//create and show alert dialog with title, message, buttons(positive,negative and neutral).</span></strong>
 </em><strong>void </strong>createAlertDialogWithButtons() {

<em><strong><span style="color:#04603e" class="has-inline-color">//create an instance of AlertDialog.Builder</span></strong>
 </em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

 <em><strong><span style="color:#04603e" class="has-inline-color">//set dialog properties
 //Set the title displayed in the Dialog.</span></strong>
 </em>builder.setTitle(R.string.<strong><em>alert_dialog_title</em></strong>)
<em><strong><span style="color:#04603e" class="has-inline-color">//Set the icon to be used in the title.</span></strong>
 </em>.setIcon(R.drawable.<strong><em>alert_icon</em></strong>)
<em><strong><span style="color:#04603e" class="has-inline-color">//Set the message to display in the Dialog.</span></strong>
 </em>.setMessage(R.string.<strong><em>dialog_message</em></strong>);

<em><strong><span style="color:#04603e" class="has-inline-color">//Set a listener to be invoked when the positive button of the dialog is pressed.</span></strong>
 </em>builder.setPositiveButton(R.string.<strong><em>dialog_btn_ok</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
@Override
<strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
<em>// User clicked OK button
 </em>dialogInterface.dismiss();
Toast.<em>makeText</em>(getApplicationContext(), <strong>"OK pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();
}
});

<em>//Set a listener to be invoked when the negative button of the dialog is pressed.
 </em>builder.setNegativeButton(R.string.<strong><em>dialog_btn_cancel</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
@Override
<strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
<em>// User clicked CANCEL button
 </em>dialogInterface.dismiss();
Toast.<em>makeText</em>(getApplicationContext(), <strong>"CANCEL pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();

}
});

<em>//Set a listener to be invoked when the neutral button of the dialog is pressed.
 </em>builder.setNeutralButton(R.string.<strong><em>dialog_btn_got_it</em></strong>, <strong>new </strong>DialogInterface.OnClickListener() {
@Override
<strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>i) {
<em>// User clicked GOT IT button
 </em>dialogInterface.dismiss();
Toast.<em>makeText</em>(getApplicationContext(), <strong>"GOT IT pressed "</strong>, Toast.<strong><em>LENGTH_SHORT</em></strong>).show();

}
});

<em>//create an AlertDialog
 </em>AlertDialog alertDialog = builder.create();

<em>//display the dialog on screen
 </em>alertDialog.show();


}

<em>//create and show alert dialog with title and single-choice list.
 </em><strong>void </strong>createAlertDialogWithList() {

<em>//create an instance of AlertDialog.Builder
 </em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

<em>//get string array (languages)
 </em><strong>final </strong>String[] language = getResources().getStringArray(R.array.<strong><em>languages</em></strong>);

builder.setTitle(getText(R.string.<strong><em>select_language_title</em></strong>))

<em>//Set language(list of languages) to be displayed in the dialog as the content.
 </em>.setItems(language, <strong>new </strong>DialogInterface.OnClickListener() {
@Override
<strong>public void </strong>onClick(DialogInterface dialogInterface, <strong>int </strong>indexPosition) {

<em>// The 'indexPosition' argument contains the index position
 // of the selected item
 </em>String selectedItem = Arrays.<em>asList</em>(language).get(indexPosition);
Toast.<em>makeText</em>(getApplicationContext(), <strong>"Selected Language: " </strong>+ selectedItem, Toast.<strong><em>LENGTH_LONG</em></strong>).show();

}
});

<em>//create an AlertDialog
 </em>AlertDialog alertDialog = builder.create();

<em>//display the dialog on screen
 </em>alertDialog.show();
}

<em>//create and show alert dialog with custom layout.
 </em><strong>void </strong>createAlertDialogWithCustomLayout() {

<em>//create an instance of AlertDialog.Builder
 </em>AlertDialog.Builder builder = <strong>new </strong>AlertDialog.Builder(<strong>this</strong>);

<em>//get view object represents custom layout of dialog.
 </em>View view = getLayoutInflater().inflate(R.layout.<strong><em>dialog_custom_layout</em></strong>, <strong>null</strong>);

<em>//get button and spinner by its id from view object.
 </em><strong>final </strong>Spinner spinner = view.findViewById(R.id.<strong><em>spinner</em></strong>);
Button btnOk = view.findViewById(R.id.<strong><em>btn_ok</em></strong>);

ArrayList<;String>; list = <strong>new </strong>ArrayList<;>;(Arrays.<em>asList</em>(getResources().getStringArray(R.array.<strong><em>languages</em></strong>)));
ArrayAdapter<;String>; adapter = <strong>new </strong>ArrayAdapter<;>;(<strong>this</strong>, android.R.layout.<strong><em>simple_spinner_item</em></strong>, list);
adapter.setDropDownViewResource(android.R.layout.<strong><em>simple_spinner_dropdown_item</em></strong>);

<em>//set adapter in spinner.
 </em>spinner.setAdapter(adapter);

<em>//Sets a custom view to be the contents of the alert dialog.
 </em>builder.setView(view);

<em>//create an AlertDialog
 </em><strong>final </strong>AlertDialog dialog = builder.create();

<em>//set click listener on OK button click
 </em>btnOk.setOnClickListener(<strong>new </strong>View.OnClickListener() {
@Override
<strong>public void </strong>onClick(View view) {
dialog.dismiss();
String selectedItem = (String) spinner.getSelectedItem();
Toast.<em>makeText</em>(getApplicationContext(), <strong>"Selected Language: " </strong>+ selectedItem, Toast.<strong><em>LENGTH_LONG</em></strong>).show();
}
});

<em>//display the dialog on screen
 </em>dialog.show();

}
}</pre>


