Skip to content

Commit

Permalink
Use WindowCompat to toggle the keyboard.
Browse files Browse the repository at this point in the history
  • Loading branch information
Isira-Seneviratne committed Nov 4, 2022
1 parent 6b87a36 commit 1878d17
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -668,15 +668,14 @@ private void showKeyboardSearch() {
if (DEBUG) {
Log.d(TAG, "showKeyboardSearch() called");
}
KeyboardUtil.showKeyboard(activity, searchEditText);
KeyboardUtil.showKeyboard(getActivity(), searchEditText);
}

private void hideKeyboardSearch() {
if (DEBUG) {
Log.d(TAG, "hideKeyboardSearch() called");
}

KeyboardUtil.hideKeyboard(activity, searchEditText);
KeyboardUtil.hideKeyboard(getActivity(), searchEditText);
}

private void showDeleteSuggestionDialog(final SuggestionItem item) {
Expand Down
33 changes: 6 additions & 27 deletions app/src/main/java/org/schabi/newpipe/util/KeyboardUtil.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
package org.schabi.newpipe.util;

import android.app.Activity;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import androidx.core.content.ContextCompat;
import androidx.core.view.WindowCompat;
import androidx.core.view.WindowInsetsCompat;

/**
* Utility class for the Android keyboard.
Expand All @@ -20,36 +20,15 @@ public static void showKeyboard(final Activity activity, final EditText editText
if (activity == null || editText == null) {
return;
}

if (editText.requestFocus()) {
final InputMethodManager imm = ContextCompat.getSystemService(activity,
InputMethodManager.class);
if (!imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED)) {
/*
* Sometimes the keyboard can't be shown because Android's ImeFocusController is in
* a incorrect state e.g. when animations are disabled or the unfocus event of the
* previous view arrives in the wrong moment (see #7647 for details).
* The invalid state can be fixed by to re-focusing the editText.
*/
editText.clearFocus();
editText.requestFocus();

// Try again
imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED);
}
}
WindowCompat.getInsetsController(activity.getWindow(), editText)
.show(WindowInsetsCompat.Type.ime());
}

public static void hideKeyboard(final Activity activity, final EditText editText) {
if (activity == null || editText == null) {
return;
}

final InputMethodManager imm = ContextCompat.getSystemService(activity,
InputMethodManager.class);
imm.hideSoftInputFromWindow(editText.getWindowToken(),
InputMethodManager.RESULT_UNCHANGED_SHOWN);

editText.clearFocus();
WindowCompat.getInsetsController(activity.getWindow(), editText)
.hide(WindowInsetsCompat.Type.ime());
}
}

0 comments on commit 1878d17

Please sign in to comment.