Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Squished dialogs when opening keyboard programmatically #1689

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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)
}
}