Merge pull request #1689 from nextcloud/fix/1660/squished-dialogs

fix: Squished dialogs when opening keyboard programmatically
This commit is contained in:
Álvaro Brey 2023-03-02 10:08:47 +01:00 committed by GitHub
commit 83c91ccc95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 27 additions and 13 deletions

View file

@ -25,6 +25,7 @@ import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil; import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.DialogChangeCategoryBinding; import it.niedermann.owncloud.notes.databinding.DialogChangeCategoryBinding;
import it.niedermann.owncloud.notes.main.navigation.NavigationItem; import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
import it.niedermann.owncloud.notes.shared.util.KeyboardUtils;
/** /**
* This {@link DialogFragment} allows for the selection of a category. * This {@link DialogFragment} allows for the selection of a category.
@ -170,12 +171,7 @@ public class CategoryDialogFragment extends BrandedDialogFragment {
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
if (editCategory.getText() == null || editCategory.getText().length() == 0) { if (editCategory.getText() == null || editCategory.getText().length() == 0) {
editCategory.requestFocus(); KeyboardUtils.showKeyboardForEditText(editCategory);
if (getDialog() != null && getDialog().getWindow() != null) {
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
} else {
Log.w(TAG, "can not set SOFT_INPUT_STATE_ALWAYAS_VISIBLE because getWindow() == null");
}
} }
} }

View file

@ -18,6 +18,7 @@ import it.niedermann.owncloud.notes.R;
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment; import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
import it.niedermann.owncloud.notes.branding.BrandingUtil; import it.niedermann.owncloud.notes.branding.BrandingUtil;
import it.niedermann.owncloud.notes.databinding.DialogEditTitleBinding; import it.niedermann.owncloud.notes.databinding.DialogEditTitleBinding;
import it.niedermann.owncloud.notes.shared.util.KeyboardUtils;
public class EditTitleDialogFragment extends BrandedDialogFragment { public class EditTitleDialogFragment extends BrandedDialogFragment {
@ -68,13 +69,7 @@ public class EditTitleDialogFragment extends BrandedDialogFragment {
@Override @Override
public void onActivityCreated(Bundle savedInstanceState) { public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState); super.onActivityCreated(savedInstanceState);
binding.title.requestFocus(); KeyboardUtils.showKeyboardForEditText(binding.title);
final var window = requireDialog().getWindow();
if (window != null) {
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
} else {
Log.w(TAG, "can not enable soft keyboard because " + Window.class.getSimpleName() + " is null.");
}
} }
public static DialogFragment newInstance(String title) { public static DialogFragment newInstance(String title) {

View file

@ -0,0 +1,23 @@
package it.niedermann.owncloud.notes.shared.util
import android.content.Context
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
object KeyboardUtils {
private const val SHOW_INPUT_DELAY_MILLIS = 100L
@JvmStatic
fun showKeyboardForEditText(editText: EditText) {
editText.requestFocus()
// needs 100ms delay to account for focus animations
editText.postDelayed({
val context = editText.context
if (context != null) {
val inputMethodManager =
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
}
}, SHOW_INPUT_DELAY_MILLIS)
}
}