TODOs: Polish AlertDialog EditText

Makes the edit text padded correctly, and makes the keyboard's enter
button actually save/add the new list/task.

Change-Id: I4cee50d3bf7de433a50f5fc4d8ae098203d7dc59
diff --git a/app/src/main/java/io/v/todos/UIUtil.java b/app/src/main/java/io/v/todos/UIUtil.java
index 57d14ec..1341359 100644
--- a/app/src/main/java/io/v/todos/UIUtil.java
+++ b/app/src/main/java/io/v/todos/UIUtil.java
@@ -7,6 +7,7 @@
 import android.content.Context;
 import android.content.DialogInterface;
 import android.graphics.Paint;
+import android.support.design.widget.TextInputLayout;
 import android.support.v7.app.AlertDialog;
 import android.support.v7.widget.RecyclerView;
 import android.text.format.DateUtils;
@@ -52,51 +53,75 @@
         return lastDialog;
     }
 
-    public static void showAddDialog(Context context, String title,
-                                     final DialogResponseListener addListener) {
-        final EditText todoItem = new EditText(context);
+    public static AlertDialog dialogMaker(Context context, String title, String defaultValue,
+                                             final DialogResponseListener listener) {
+        // Prepare the edit text.
+        TextInputLayout inputLayout = (TextInputLayout)LayoutInflater.from(context).
+                inflate(R.layout.dialog_edittext, null);
+        final EditText editText = inputLayout.getEditText();
+        boolean adding = (defaultValue == null);
+        if (!adding) {
+            editText.setText(defaultValue);
+        }
 
-        AlertDialog dialog = new AlertDialog.Builder(context)
+        // Build the alert dialog.
+        AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(context)
                 .setTitle(title)
-                .setView(todoItem)
-                .setPositiveButton("Add", new DialogInterface.OnClickListener() {
+                .setView(inputLayout)
+                .setPositiveButton(adding ? "Add" : "Save", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) {
-                        addListener.handleResponse(todoItem.getText().toString());
+                        String response = editText.getText().toString();
+                        if (response != null && response.length() > 0) {
+                            listener.handleResponse(response);
+                        }
                     }
                 })
                 .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                     public void onClick(DialogInterface dialog, int whichButton) {
                     }
-                }).show();
+                });
+        if (!adding) {
+            // Only items being edited can be deleted.
+            // TODO(alexfandrianto): Should we keep this option? We can also swipe in order to
+            // delete tasks/lists.
+            dialogBuilder.setNeutralButton("Delete", new DialogInterface.OnClickListener() {
+                public void onClick(DialogInterface dialog, int whichButton) {
+                    listener.handleDelete();
+                }
+            });
+        }
+
+        // Show the dialog with the keyboard up. If the "Send" button is pressed, treat that as a
+        // positive button press.
+        final AlertDialog dialog = dialogBuilder.show();
         dialog.getWindow().setSoftInputMode(
                 WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+        editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
+            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
+                if ((event != null && (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) ||
+                        (actionId == EditorInfo.IME_ACTION_DONE)) {
+                    dialog.getButton(DialogInterface.BUTTON_POSITIVE).performClick();
+                    return true;
+                }
+                return false;
+            }
+        });
+
+        return dialog;
+    }
+
+    public static void showAddDialog(Context context, String title,
+                                     final DialogResponseListener addListener) {
+        AlertDialog dialog = dialogMaker(context, title, null, addListener);
+
         lastDialog = dialog;
     }
 
     public static void showEditDialog(Context context, String title, String defaultValue,
                                       final DialogResponseListener editListener) {
-        final EditText todoItem = new EditText(context);
-        todoItem.setText(defaultValue);
 
-        AlertDialog dialog = new AlertDialog.Builder(context)
-                .setTitle(title)
-                .setView(todoItem)
-                .setPositiveButton("Save", new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int whichButton) {
-                        editListener.handleResponse(todoItem.getText().toString());
-                    }
-                })
-                .setNeutralButton("Delete", new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int whichButton) {
-                        editListener.handleDelete();
-                    }
-                })
-                .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
-                    public void onClick(DialogInterface dialog, int whichButton) {
-                    }
-                }).show();
-        dialog.getWindow().setSoftInputMode(
-                WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
+        AlertDialog dialog = dialogMaker(context, title, defaultValue, editListener);
+
         lastDialog = dialog;
     }
 
diff --git a/app/src/main/res/layout/dialog_edittext.xml b/app/src/main/res/layout/dialog_edittext.xml
new file mode 100644
index 0000000..272134b
--- /dev/null
+++ b/app/src/main/res/layout/dialog_edittext.xml
@@ -0,0 +1,19 @@
+<?xml version="1.0" encoding="utf-8"?>
+<android.support.design.widget.TextInputLayout
+    xmlns:android="http://schemas.android.com/apk/res/android"
+    android:layout_width="match_parent"
+    android:layout_height="wrap_content"
+    android:paddingEnd="20dp"
+    android:paddingStart="20dp"
+    android:paddingTop="8dp">
+
+    <EditText
+        android:layout_width="fill_parent"
+        android:layout_height="wrap_content"
+        android:layout_gravity="bottom"
+        android:imeOptions="actionDone"
+        android:inputType="text"
+        android:textColor="#000000"
+        android:textSize="22sp"
+        android:textStyle="bold"/>
+</android.support.design.widget.TextInputLayout>
\ No newline at end of file