From db7de897e08700509026cc2ebb7a4963ccde0b56 Mon Sep 17 00:00:00 2001 From: Phillip Hsu Date: Tue, 30 Aug 2016 19:57:59 -0700 Subject: [PATCH] Show keyboard on AddLabelDialog show --- .../com/philliphsu/clock2/AddLabelDialog.java | 37 ++++++++++++++++--- .../clock2/timers/TimerViewHolder.java | 2 +- .../philliphsu/clock2/util/KeyboardUtils.java | 8 ++++ 3 files changed, 41 insertions(+), 6 deletions(-) diff --git a/app/src/main/java/com/philliphsu/clock2/AddLabelDialog.java b/app/src/main/java/com/philliphsu/clock2/AddLabelDialog.java index 0be651d..525982e 100644 --- a/app/src/main/java/com/philliphsu/clock2/AddLabelDialog.java +++ b/app/src/main/java/com/philliphsu/clock2/AddLabelDialog.java @@ -4,10 +4,14 @@ import android.app.Dialog; import android.content.DialogInterface; import android.os.Bundle; import android.support.annotation.NonNull; +import android.support.annotation.Nullable; import android.support.v7.app.AlertDialog; import android.support.v7.app.AppCompatDialogFragment; +import android.view.inputmethod.EditorInfo; import android.widget.EditText; +import static com.philliphsu.clock2.util.KeyboardUtils.showKeyboard; + /** * Created by Phillip Hsu on 8/30/2016. * @@ -22,7 +26,7 @@ public class AddLabelDialog extends AppCompatDialogFragment { private CharSequence mInitialText; public interface OnLabelSetListener { - void onLabelSet(CharSequence label); + void onLabelSet(String label); } /** @@ -35,16 +39,27 @@ public class AddLabelDialog extends AppCompatDialogFragment { return dialog; } + @Override + public void onCreate(@Nullable Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + } + @NonNull @Override public Dialog onCreateDialog(Bundle savedInstanceState) { mEditText = new EditText(getActivity()); mEditText.setText(mInitialText); - mEditText.setSelection(0, mEditText.length()); + mEditText.setInputType( + EditorInfo.TYPE_CLASS_TEXT // Needed or else we won't get automatic spacing between words + | EditorInfo.TYPE_TEXT_FLAG_CAP_SENTENCES); - return new AlertDialog.Builder(getActivity(), getTheme()) + // TODO: We can use the same value for both directions. + int spacingLeft = getResources().getDimensionPixelSize(R.dimen.item_padding_start); + int spacingRight = getResources().getDimensionPixelSize(R.dimen.item_padding_end); + + final AlertDialog alert = new AlertDialog.Builder(getActivity(), getTheme()) .setTitle(R.string.label) - .setView(mEditText) + .setView(mEditText, spacingLeft, 0, spacingRight, 0) .setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { @@ -55,11 +70,23 @@ public class AddLabelDialog extends AppCompatDialogFragment { @Override public void onClick(DialogInterface dialog, int which) { if (mOnLabelSetListener != null) { - mOnLabelSetListener.onLabelSet(mEditText.getText()); + // If we passed the text back as an Editable (subtype of CharSequence + // used in EditText), then there may be text formatting left in there, + // which we don't want. + mOnLabelSetListener.onLabelSet(mEditText.getText().toString()); } dismiss(); } }) .create(); + + alert.setOnShowListener(new DialogInterface.OnShowListener() { + @Override + public void onShow(DialogInterface dialog) { + showKeyboard(getActivity(), mEditText); + mEditText.setSelection(0, mEditText.length()); + } + }); + return alert; } } diff --git a/app/src/main/java/com/philliphsu/clock2/timers/TimerViewHolder.java b/app/src/main/java/com/philliphsu/clock2/timers/TimerViewHolder.java index f66b878..60c2ddc 100644 --- a/app/src/main/java/com/philliphsu/clock2/timers/TimerViewHolder.java +++ b/app/src/main/java/com/philliphsu/clock2/timers/TimerViewHolder.java @@ -75,7 +75,7 @@ public class TimerViewHolder extends BaseViewHolder { void openLabelEditor() { AddLabelDialog dialog = AddLabelDialog.newInstance(new AddLabelDialog.OnLabelSetListener() { @Override - public void onLabelSet(CharSequence label) { + public void onLabelSet(String label) { mLabel.setText(label); // TODO: persist change. Use TimerController and its update() } diff --git a/app/src/main/java/com/philliphsu/clock2/util/KeyboardUtils.java b/app/src/main/java/com/philliphsu/clock2/util/KeyboardUtils.java index 81a4541..e5fedc2 100644 --- a/app/src/main/java/com/philliphsu/clock2/util/KeyboardUtils.java +++ b/app/src/main/java/com/philliphsu/clock2/util/KeyboardUtils.java @@ -19,4 +19,12 @@ public class KeyboardUtils { } } + /** + * @param c The Context to retrieve the system's input method service + * @param v The currently focused view, which would like to receive soft keyboard input. + */ + public static void showKeyboard(Context c, View v) { + InputMethodManager imm = (InputMethodManager) c.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(v, InputMethodManager.SHOW_IMPLICIT); + } }