From f5ba5f4cf0fe7596d68df4d7413667a22a305d28 Mon Sep 17 00:00:00 2001 From: MohitMaliFtechiz Date: Thu, 1 Jun 2023 19:00:46 +0530 Subject: [PATCH] Fixes of toggleSoftInput(Int, Int) method of InputMethodManager is deprecated --- .../core/extensions/FragmentExtensions.kt | 2 +- .../kiwix/kiwixmobile/core/main/AddNoteDialog.kt | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt index eeadaa799..4a37ed58b 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/extensions/FragmentExtensions.kt @@ -39,7 +39,7 @@ fun Fragment.toast(stringId: Int, length: Int = Toast.LENGTH_LONG) { fun Fragment.closeKeyboard() { val inputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) + inputMethodManager.hideSoftInputFromWindow(requireView().windowToken, 0) } fun View.closeKeyboard() { diff --git a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt index f80e0e58e..5e8f8757c 100644 --- a/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt +++ b/core/src/main/java/org/kiwix/kiwixmobile/core/main/AddNoteDialog.kt @@ -32,6 +32,7 @@ import android.view.MenuItem import android.view.View import android.view.ViewGroup import android.view.inputmethod.InputMethodManager +import android.widget.EditText import android.widget.Toast import androidx.activity.OnBackPressedCallback import androidx.appcompat.widget.Toolbar @@ -289,15 +290,22 @@ class AddNoteDialog : DialogFragment() { ) if (!noteFileExists) { // Prepare for input in case of empty/new note - dialogNoteAddNoteBinding?.addNoteEditText?.requestFocus() - showKeyboard() + dialogNoteAddNoteBinding?.addNoteEditText?.apply { + requestFocus() + showKeyboard(this) + } } } - private fun showKeyboard() { + @Suppress("MagicNumber") + private fun showKeyboard(editText: EditText) { val inputMethodManager = requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager - inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0) + editText.postDelayed( + { + inputMethodManager.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT) + }, 100 + ) } private fun saveNote(noteText: String) {