Introduction

When developing an app, one of the basic ways to show a user something important is through a dialog.
A dialog is a small popup message with one or two buttons to show the user an important message or to ask the user for some input.
There are various ways to generate a simple dialog. I will be showing four general-purpose dialogs that I have used the most.
Simple Dialog, Date picker, Time Picker and using my own layout in a dialog.
Let's create a sample project.
From the previous articles, we have borrowed the "button_background.xml" file in the drawable folder.
drawable/button_background.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >
  3. <!-- disabled -->
  4. <item android:state_enabled="false" >
  5. <shape android:shape="rectangle">
  6. <gradient
  7. android:startColor="#454545"
  8. android:endColor="#454545"
  9. android:angle="-90"
  10. android:type="linear"
  11. />
  12. <corners android:radius="5dp" />
  13. </shape>
  14. </item>
  15. <!-- pressed -->
  16. <item android:state_pressed="true" android:state_enabled="true" >
  17. <shape android:shape="rectangle">
  18. <gradient
  19. android:startColor="#64334C"
  20. android:endColor="#300019"
  21. android:angle="-90"
  22. android:type="linear"
  23. />
  24. <corners android:radius="5dp" />
  25. </shape>
  26. </item>
  27. <!-- focused -->
  28. <item android:state_focused="true">
  29. <shape android:shape="rectangle">
  30. <gradient
  31. android:startColor="#C76699"
  32. android:endColor="#610033"
  33. android:angle="-90"
  34. android:type="linear"
  35. />
  36. <corners android:radius="5dp" />
  37. <stroke android:width="2dp" android:color="#dddddd"/>
  38. </shape>
  39. </item>
  40. <!-- default -->
  41. <item>
  42. <shape android:shape="rectangle">
  43. <gradient
  44. android:startColor="#C76699"
  45. android:endColor="#610033"
  46. android:angle="-90"
  47. android:type="linear"
  48. />
  49. <corners android:radius="5dp" />
  50. </shape>
  51. </item>
  52. </selector>
Now open the activity_main.xml file and add four buttons to it. These buttons will be used to show dialogs.
activity_main.xml
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. tools:context=".MainActivity"
  6. android:orientation="vertical"
  7. >
  8. <Button
  9. android:id="@+id/simpleDialogButton"
  10. android:layout_height="wrap_content"
  11. android:layout_width="match_parent"
  12. android:layout_margin="10dp"
  13. android:text="Simple Dialog"
  14. android:background="@drawable/button_background"
  15. android:textColor="#ffffff"
  16. />
  17. <Button
  18. android:id="@+id/timeDialogButton"
  19. android:layout_height="wrap_content"
  20. android:layout_width="match_parent"
  21. android:layout_margin="10dp"
  22. android:text="Time Dialog"
  23. android:background="@drawable/button_background"
  24. android:textColor="#ffffff"
  25. />
  26. <Button
  27. android:id="@+id/dateDialogButton"
  28. android:layout_height="wrap_content"
  29. android:layout_width="match_parent"
  30. android:layout_margin="10dp"
  31. android:text="Date Dialog"
  32. android:background="@drawable/button_background"
  33. android:textColor="#ffffff"
  34. />
  35. <Button
  36. android:id="@+id/customDialogButton"
  37. android:layout_height="wrap_content"
  38. android:layout_width="match_parent"
  39. android:layout_margin="10dp"
  40. android:text="Custom Dialog"
  41. android:background="@drawable/button_background"
  42. android:textColor="#ffffff"
  43. />
  44. </LinearLayout>
For one of our custom layouts create a file custom_dialog_layout.xml in the layout folder.
layout/custom_dialog_layout.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="vertical"
  6. android:gravity="center_horizontal">
  7. <RadioGroup
  8. android:id="@+id/radioGroup1"
  9. android:layout_width="wrap_content"
  10. android:layout_height="wrap_content"
  11. >
  12. <RadioButton
  13. android:id="@+id/radio0"
  14. android:layout_width="wrap_content"
  15. android:layout_height="wrap_content"
  16. android:checked="true"
  17. android:text="Option1" />
  18. <RadioButton
  19. android:id="@+id/radio1"
  20. android:layout_width="wrap_content"
  21. android:layout_height="wrap_content"
  22. android:text="Option2" />
  23. <RadioButton
  24. android:id="@+id/radio2"
  25. android:layout_width="wrap_content"
  26. android:layout_height="wrap_content"
  27. android:text="Option3" />
  28. </RadioGroup>
  29. <Button
  30. android:id="@+id/okButton"
  31. android:layout_height="wrap_content"
  32. android:layout_width="100dp"
  33. android:padding="10dp"
  34. android:text="Ok"
  35. />
  36. </LinearLayout>
Now MainActivity.java has all the code you need to make the dialogs, for proper clarification of the syntax please check the comments in the code.
  1. public class MainActivity extends Activity {
  2. @Override
  3. protected void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. setContentView(R.layout.activity_main);
  6. /*
  7. * Add listeners to the buttons and call the appropriate methods
  8. */
  9. ((Button)findViewById(R.id.simpleDialogButton)).setOnClickListener(new OnClickListener() {
  10. @Override
  11. public void onClick(View v) {
  12. // TODO Auto-generated method stub
  13. createSimpleDialog();
  14. }
  15. });
  16. ((Button)findViewById(R.id.dateDialogButton)).setOnClickListener(new OnClickListener() {
  17. @Override
  18. public void onClick(View v) {
  19. // TODO Auto-generated method stub
  20. createDateDialog();
  21. }
  22. });
  23. ((Button)findViewById(R.id.timeDialogButton)).setOnClickListener(new OnClickListener() {
  24. @Override
  25. public void onClick(View v) {
  26. // TODO Auto-generated method stub
  27. createTimeDialog();
  28. }
  29. });
  30. ((Button)findViewById(R.id.customDialogButton)).setOnClickListener(new OnClickListener() {
  31. @Override
  32. public void onClick(View v) {
  33. // TODO Auto-generated method stub
  34. createCustomDialog();
  35. }
  36. });
  37. }
  38. /*
  39. * This is a convenient method to show a toast after dismissing the dialog
  40. */
  41. private void showToast(String toastString)
  42. {
  43. Toast.makeText(MainActivity.this, toastString, Toast.LENGTH_SHORT).show();
  44. }
  45. /*
  46. * This is the basic method to create a dialog
  47. * This sets a message and two buttons to the dialog
  48. */
  49. private void createSimpleDialog()
  50. {
  51. AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
  52. builder.setMessage("This is simple dialog")
  53. .setPositiveButton("Ok", new DialogInterface.OnClickListener() {
  54. public void onClick(DialogInterface dialog, int id) {
  55. showToast("Simple dialog Ok button pressed");
  56. }
  57. })
  58. .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  59. public void onClick(DialogInterface dialog, int id) {
  60. showToast("Simple dialog Cancel button pressed");
  61. }
  62. });
  63. builder.create().show();
  64. }
  65. /*
  66. * This method used the builtin DatePickerDialog to ask user to select a date
  67. * We use a Calendar instance to get the current date, we also add a listener(OnDateSetListener) to the dialog to get the selected date
  68. */
  69. private void createDateDialog(){
  70. Calendar c = Calendar.getInstance();
  71. DatePickerDialog dialog = new DatePickerDialog(MainActivity.this, mDateSetListener, c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DAY_OF_MONTH));
  72. dialog.show();
  73. }
  74. /*
  75. * This listener is called when we select date in the DatePickerDialog
  76. */
  77. OnDateSetListener mDateSetListener = new OnDateSetListener() {
  78. @Override
  79. public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
  80. // TODO Auto-generated method stub
  81. showToast("Selected date: "+dayOfMonth+"/"+(monthOfYear+1)+"/"+year);
  82. }
  83. };
  84. /*
  85. * This method used the builtin TimePickerDialog to ask user to select a date
  86. * We use a Calendar instance to get the current time, we also add a listener(OnTimeSetListener) to the dialog to get the selected time
  87. */
  88. private void createTimeDialog() {
  89. Calendar c = Calendar.getInstance();
  90. TimePickerDialog dialog = new TimePickerDialog(MainActivity.this, mTimeSetListener, c.get(Calendar.HOUR), c.get(Calendar.MINUTE), false);
  91. dialog.show();
  92. }
  93. /*
  94. * This listener is called when we select time in the TimePickerDialog
  95. */
  96. OnTimeSetListener mTimeSetListener = new OnTimeSetListener() {
  97. @Override
  98. public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
  99. // TODO Auto-generated method stub
  100. showToast("Selected time: "+hourOfDay+":"+minute);
  101. }
  102. };
  103. /*
  104. * This method creates a custom dialog using the layout we has created previously
  105. */
  106. private void createCustomDialog(){
  107. //Create a dialog object
  108. final Dialog dialog = new Dialog(MainActivity.this);
  109. //Set its layout
  110. dialog.setContentView(R.layout.custom_dialog_layout);
  111. //Set the title
  112. dialog.setTitle("This is custom layout");
  113. //Make it cancelable
  114. dialog.setCancelable(true);
  115. //We need to dismiss the dialog so we add a listener to the ok button
  116. dialog.findViewById(R.id.okButton).setOnClickListener(new OnClickListener() {
  117. @Override
  118. public void onClick(View v) {
  119. // TODO Auto-generated method stub
  120. dialog.dismiss();
  121. }
  122. });
  123. dialog.show();
  124. }
  125. }
This is the very basic code to make an interesting UI. We can always use the custom dialogs to create a very user interactive app but don't overdo it or it may end up being too much of a hassle for the user.
Screenshots:
Article5Img1.png
Article5Img2.png
Article5Img3.png
Article5Img4.png