Show keyboard on AddLabelDialog show

This commit is contained in:
Phillip Hsu 2016-08-30 19:57:59 -07:00
parent 1a9471d3c7
commit db7de897e0
3 changed files with 41 additions and 6 deletions

View File

@ -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;
}
}

View File

@ -75,7 +75,7 @@ public class TimerViewHolder extends BaseViewHolder<Timer> {
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()
}

View File

@ -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);
}
}