Fixes of toggleSoftInput(Int, Int) method of InputMethodManager is deprecated

This commit is contained in:
MohitMaliFtechiz 2023-06-01 19:00:46 +05:30 committed by Kelson
parent 66a535111c
commit f5ba5f4cf0
2 changed files with 13 additions and 5 deletions

View File

@ -39,7 +39,7 @@ fun Fragment.toast(stringId: Int, length: Int = Toast.LENGTH_LONG) {
fun Fragment.closeKeyboard() { fun Fragment.closeKeyboard() {
val inputMethodManager = val inputMethodManager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0) inputMethodManager.hideSoftInputFromWindow(requireView().windowToken, 0)
} }
fun View.closeKeyboard() { fun View.closeKeyboard() {

View File

@ -32,6 +32,7 @@ import android.view.MenuItem
import android.view.View import android.view.View
import android.view.ViewGroup import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.Toast import android.widget.Toast
import androidx.activity.OnBackPressedCallback import androidx.activity.OnBackPressedCallback
import androidx.appcompat.widget.Toolbar import androidx.appcompat.widget.Toolbar
@ -289,15 +290,22 @@ class AddNoteDialog : DialogFragment() {
) )
if (!noteFileExists) { if (!noteFileExists) {
// Prepare for input in case of empty/new note // Prepare for input in case of empty/new note
dialogNoteAddNoteBinding?.addNoteEditText?.requestFocus() dialogNoteAddNoteBinding?.addNoteEditText?.apply {
showKeyboard() requestFocus()
showKeyboard(this)
}
} }
} }
private fun showKeyboard() { @Suppress("MagicNumber")
private fun showKeyboard(editText: EditText) {
val inputMethodManager = val inputMethodManager =
requireContext().getSystemService(Context.INPUT_METHOD_SERVICE) as 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) { private fun saveNote(noteText: String) {