From f3be89b503649067ba33638a3881c987adc3ec42 Mon Sep 17 00:00:00 2001 From: litetex <40789489+litetex@users.noreply.github.com> Date: Fri, 24 Dec 2021 21:30:49 +0100 Subject: [PATCH] Abstracted methods for the Android keyboard --- .../fragments/list/search/SearchFragment.java | 23 ++-------- .../org/schabi/newpipe/util/KeyboardUtil.java | 43 +++++++++++++++++++ 2 files changed, 46 insertions(+), 20 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/util/KeyboardUtil.java diff --git a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java index 15424334d..055c27733 100644 --- a/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java +++ b/app/src/main/java/org/schabi/newpipe/fragments/list/search/SearchFragment.java @@ -25,7 +25,6 @@ import android.view.View; import android.view.ViewGroup; import android.view.animation.DecelerateInterpolator; import android.view.inputmethod.EditorInfo; -import android.view.inputmethod.InputMethodManager; import android.widget.EditText; import android.widget.TextView; @@ -34,7 +33,6 @@ import androidx.annotation.Nullable; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AlertDialog; import androidx.appcompat.widget.TooltipCompat; -import androidx.core.content.ContextCompat; import androidx.core.text.HtmlCompat; import androidx.preference.PreferenceManager; import androidx.recyclerview.widget.ItemTouchHelper; @@ -65,6 +63,7 @@ import org.schabi.newpipe.settings.NewPipeSettings; import org.schabi.newpipe.util.Constants; import org.schabi.newpipe.util.DeviceUtils; import org.schabi.newpipe.util.ExtractorHelper; +import org.schabi.newpipe.util.KeyboardUtil; import org.schabi.newpipe.util.NavigationHelper; import org.schabi.newpipe.util.ServiceHelper; @@ -670,31 +669,15 @@ public class SearchFragment extends BaseListFragment + * See also https://stackoverflow.com/q/1109022 + *

+ */ +public final class KeyboardUtil { + private KeyboardUtil() { + } + + 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); + imm.showSoftInput(editText, InputMethodManager.SHOW_FORCED); + } + } + + 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(); + } +}