I have EditText in BottomSheet. When BottomSheet show and I tap the EditText then the soft keyboard is showing. But how I can hide the soft keyboard when the length of value Edittext is 6 in BottomSheet?
I have some logic like this:
private fun showBottomSheet() {
val binding: BottomSheetSetupEasyPinBinding =
DataBindingUtil.inflate(LayoutInflater.from(activity), R.layout.bottom_sheet_setup_easy_pin, null, false)
binding.fragment = this
binding.vm = vm
binding.lifecycleOwner = this
//For hide softKeyboard
binding.etEasyPinConfirmation.addTextChangedListener(object : TextWatcher {
override fun afterTextChanged(s: Editable) {
}
override fun beforeTextChanged(s: CharSequence, start: Int,
count: Int, after: Int) {
}
override fun onTextChanged(s: CharSequence, start: Int,
before: Int, count: Int) {
if (s.length == 6) {
hideSoftKeyboard()
Toast.makeText(activity, "Length is 6", Toast.LENGTH_SHORT).show()
}
}
})
bottomSheet.setContentView(binding.root)
bottomSheet.setCancelable(false)
bottomSheet.show()
}
And this is the function for hiding soft keyboard:
fun hideSoftKeyboard() {
inputMethodManager.hideSoftInputFromWindow(view!!.windowToken, 0)
}
And this is the global variable and declaration that variable in onViewCreated
:
// global variable
private lateinit var inputMethodManager : InputMethodManager
..
// in onViewCreated
inputMethodManager = requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
But when the length of value EditText is 6, the Toast is showing, and I already debugging it the function is executed, but the keyboard not hide. Does anyone know why my code is not working for hiding a soft keyboard in BottomSheet? Because if EditText is not in BottomSheet, this function is a success for hiding the soft keyboard
I just used the next code:
fun hideSoftKeyboardBottomSheet(view: View) {
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0)
}
and insert the view form Bottom Sheet into this function