From 5bc78f5bbd8163fef66c4fd56f6efcd1d1f2e368 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=81lvaro=20Brey?= Date: Thu, 23 Feb 2023 11:30:41 +0100 Subject: [PATCH] fix: Squished dialogs when opening keyboard programmatically MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Álvaro Brey --- .../edit/category/CategoryDialogFragment.java | 8 +--- .../edit/title/EditTitleDialogFragment.java | 9 +--- .../notes/shared/util/KeyboardUtils.kt | 45 +++++++++++++++++++ 3 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 app/src/main/java/it/niedermann/owncloud/notes/shared/util/KeyboardUtils.kt diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java index 8e904556a..b29f14cf8 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java @@ -25,6 +25,7 @@ import it.niedermann.owncloud.notes.branding.BrandingUtil; import it.niedermann.owncloud.notes.databinding.DialogChangeCategoryBinding; 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. @@ -170,12 +171,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) { public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); if (editCategory.getText() == null || editCategory.getText().length() == 0) { - editCategory.requestFocus(); - 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"); - } + KeyboardUtils.showKeyboardForEditText(editCategory); } } diff --git a/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java b/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java index d9b023fbe..b370f7982 100644 --- a/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java +++ b/app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java @@ -18,6 +18,7 @@ import it.niedermann.owncloud.notes.branding.BrandedDialogFragment; import it.niedermann.owncloud.notes.branding.BrandingUtil; import it.niedermann.owncloud.notes.databinding.DialogEditTitleBinding; +import it.niedermann.owncloud.notes.shared.util.KeyboardUtils; public class EditTitleDialogFragment extends BrandedDialogFragment { @@ -68,13 +69,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) { @Override public void onActivityCreated(Bundle savedInstanceState) { super.onActivityCreated(savedInstanceState); - binding.title.requestFocus(); - 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."); - } + KeyboardUtils.showKeyboardForEditText(binding.title); } public static DialogFragment newInstance(String title) { diff --git a/app/src/main/java/it/niedermann/owncloud/notes/shared/util/KeyboardUtils.kt b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/KeyboardUtils.kt new file mode 100644 index 000000000..3fac73d3c --- /dev/null +++ b/app/src/main/java/it/niedermann/owncloud/notes/shared/util/KeyboardUtils.kt @@ -0,0 +1,45 @@ +/* + * Nextcloud Android client application + * + * @author Álvaro Brey + * Copyright (C) 2022 Álvaro Brey + * Copyright (C) 2022 Nextcloud GmbH + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE + * License as published by the Free Software Foundation; either + * version 3 of the License, or any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU AFFERO GENERAL PUBLIC LICENSE for more details. + * + * You should have received a copy of the GNU Affero General Public + * License along with this program. If not, see . + * + */ + +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) + } +}