Skip to content

Commit 83c91cc

Browse files
authored
Merge pull request #1689 from nextcloud/fix/1660/squished-dialogs
fix: Squished dialogs when opening keyboard programmatically
2 parents 42d7331 + d6e4980 commit 83c91cc

File tree

3 files changed

+27
-13
lines changed

3 files changed

+27
-13
lines changed

app/src/main/java/it/niedermann/owncloud/notes/edit/category/CategoryDialogFragment.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import it.niedermann.owncloud.notes.branding.BrandingUtil;
2626
import it.niedermann.owncloud.notes.databinding.DialogChangeCategoryBinding;
2727
import it.niedermann.owncloud.notes.main.navigation.NavigationItem;
28+
import it.niedermann.owncloud.notes.shared.util.KeyboardUtils;
2829

2930
/**
3031
* This {@link DialogFragment} allows for the selection of a category.
@@ -170,12 +171,7 @@ public void onSaveInstanceState(@NonNull Bundle outState) {
170171
public void onActivityCreated(Bundle savedInstanceState) {
171172
super.onActivityCreated(savedInstanceState);
172173
if (editCategory.getText() == null || editCategory.getText().length() == 0) {
173-
editCategory.requestFocus();
174-
if (getDialog() != null && getDialog().getWindow() != null) {
175-
getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
176-
} else {
177-
Log.w(TAG, "can not set SOFT_INPUT_STATE_ALWAYAS_VISIBLE because getWindow() == null");
178-
}
174+
KeyboardUtils.showKeyboardForEditText(editCategory);
179175
}
180176
}
181177

app/src/main/java/it/niedermann/owncloud/notes/edit/title/EditTitleDialogFragment.java

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import it.niedermann.owncloud.notes.branding.BrandedDialogFragment;
1919
import it.niedermann.owncloud.notes.branding.BrandingUtil;
2020
import it.niedermann.owncloud.notes.databinding.DialogEditTitleBinding;
21+
import it.niedermann.owncloud.notes.shared.util.KeyboardUtils;
2122

2223
public class EditTitleDialogFragment extends BrandedDialogFragment {
2324

@@ -68,13 +69,7 @@ public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
6869
@Override
6970
public void onActivityCreated(Bundle savedInstanceState) {
7071
super.onActivityCreated(savedInstanceState);
71-
binding.title.requestFocus();
72-
final var window = requireDialog().getWindow();
73-
if (window != null) {
74-
window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
75-
} else {
76-
Log.w(TAG, "can not enable soft keyboard because " + Window.class.getSimpleName() + " is null.");
77-
}
72+
KeyboardUtils.showKeyboardForEditText(binding.title);
7873
}
7974

8075
public static DialogFragment newInstance(String title) {
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package it.niedermann.owncloud.notes.shared.util
2+
3+
import android.content.Context
4+
import android.view.inputmethod.InputMethodManager
5+
import android.widget.EditText
6+
7+
object KeyboardUtils {
8+
private const val SHOW_INPUT_DELAY_MILLIS = 100L
9+
10+
@JvmStatic
11+
fun showKeyboardForEditText(editText: EditText) {
12+
editText.requestFocus()
13+
// needs 100ms delay to account for focus animations
14+
editText.postDelayed({
15+
val context = editText.context
16+
if (context != null) {
17+
val inputMethodManager =
18+
context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
19+
inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT)
20+
}
21+
}, SHOW_INPUT_DELAY_MILLIS)
22+
}
23+
}

0 commit comments

Comments
 (0)