From 5059abf2d08a80df787dc346186e3caab1e8cd32 Mon Sep 17 00:00:00 2001 From: Jakob Nixdorf Date: Wed, 3 Jan 2018 16:21:29 +0100 Subject: [PATCH] Automatically show and hide keyboard in CredentialsPreference --- .../Preferences/CredentialsPreference.java | 13 +++++++++++++ .../flocke/andotp/Utilities/UIHelper.java | 16 ++++++++++++++++ 2 files changed, 29 insertions(+) diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Preferences/CredentialsPreference.java b/app/src/main/java/org/shadowice/flocke/andotp/Preferences/CredentialsPreference.java index ac16fccc..320982ec 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Preferences/CredentialsPreference.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Preferences/CredentialsPreference.java @@ -44,6 +44,7 @@ import android.widget.Toast; import org.shadowice.flocke.andotp.R; import org.shadowice.flocke.andotp.Utilities.Settings; +import org.shadowice.flocke.andotp.Utilities.UIHelper; import java.util.Arrays; import java.util.List; @@ -220,6 +221,9 @@ public class CredentialsPreference extends DialogPreference private void updateLayout() { if (value == AuthMethod.NONE) { credentialsLayout.setVisibility(View.GONE); + + UIHelper.hideKeyboard(getContext(), getDialog().getCurrentFocus()); + btnSave.setEnabled(true); } else if (value == AuthMethod.PASSWORD) { credentialsLayout.setVisibility(View.VISIBLE); @@ -233,6 +237,9 @@ public class CredentialsPreference extends DialogPreference passwordInput.setTransformationMethod(new PasswordTransformationMethod()); passwordConfirm.setTransformationMethod(new PasswordTransformationMethod()); + passwordInput.requestFocus(); + UIHelper.showKeyboard(getContext(), passwordInput); + btnSave.setEnabled(false); } else if (value == AuthMethod.PIN) { credentialsLayout.setVisibility(View.VISIBLE); @@ -246,9 +253,15 @@ public class CredentialsPreference extends DialogPreference passwordInput.setTransformationMethod(new PasswordTransformationMethod()); passwordConfirm.setTransformationMethod(new PasswordTransformationMethod()); + passwordInput.requestFocus(); + UIHelper.showKeyboard(getContext(), passwordInput); + btnSave.setEnabled(false); } else if (value == AuthMethod.DEVICE) { credentialsLayout.setVisibility(View.GONE); + + UIHelper.hideKeyboard(getContext(), getDialog().getCurrentFocus()); + btnSave.setEnabled(true); } } diff --git a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/UIHelper.java b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/UIHelper.java index 519624ab..89fa0407 100644 --- a/app/src/main/java/org/shadowice/flocke/andotp/Utilities/UIHelper.java +++ b/app/src/main/java/org/shadowice/flocke/andotp/Utilities/UIHelper.java @@ -25,6 +25,8 @@ package org.shadowice.flocke.andotp.Utilities; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; +import android.view.View; +import android.view.inputmethod.InputMethodManager; public class UIHelper { public static void showGenericDialog(Context context, int titleId, int messageId) { @@ -39,4 +41,18 @@ public class UIHelper { .create() .show(); } + + public static void showKeyboard(Context context, View view) { + if (view != null) { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.showSoftInput(view, 0); + } + } + + public static void hideKeyboard(Context context, View view) { + if (view != null) { + InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); + imm.hideSoftInputFromWindow(view.getWindowToken(), 0); + } + } }