0

I have two edit text in my fragment where users can enter their age and weight. Based on age and weight I calculate how much water user should drink

When I try to call hide keyboard function on afterTextChanged, the keyboard gets hidden after user typed her first character.

How can I hide the keyboard when user actually finished typing?

P.S: there is a maxLength in my edit text 2 for age and 3 for weight, maybe this could be useful information when you are thinking about your recommendation

My code for edit texts with onChange extension function

        //change listener extension for TextInputEditText
    fun TextInputEditText.onChange(cb: (String) -> Unit) {

        this.addTextChangedListener(object : TextWatcher {

            override fun afterTextChanged(s: Editable?) {
                cb(s.toString())
                hideKeyboard()
            }

            override fun beforeTextChanged(
                s: CharSequence?,
                start: Int,
                count: Int,
                after: Int
            ) {
            }

            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
            }

        })
    }

    // change listener is called with agehandler function with user type age
    // I check if the edit text is empty or not
    binding.ageEditText.onChange {
        if (binding.ageEditText.text.isNullOrEmpty()) {
            Snackbar.make(binding.root, "Please type your age", Snackbar.LENGTH_SHORT).show()
        } else dashboardViewModel.ageHandler(it)
    }

fun hideKeyboard() {

    val imm = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view!!.windowToken, 0)
}
sinan
  • 570
  • 5
  • 24

1 Answers1

0

This may not be the cleanest way in the world to do things but I use a function to hide the keyboard when users press outside of the edit text. You could also look into hiding upon hitting the enter or submit key on the keyboard here

Otherwise to use these you would just say

editText.hideKeyBoardOnPressAway()
editText.addCharacterMax(2)
fun View.hideKeyBoardOnPressAway(){
    this.onFocusChangeListener = keyboardHider
}

private val keyboardHider = View.OnFocusChangeListener { view, b ->
    if (!b) { hideKeyboardFrom(view) }
}

private fun hideKeyboardFrom(view: View) {
    val imm = view.context.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
}
fun EditText.addCharacterMax(max: Int){
    filters = arrayOf(InputFilter.LengthFilter(max))
}
Eli Dangerfield
  • 154
  • 1
  • 5
  • Thanks eli, I tried it but didnt work for me. When I click inside edit text keyboard is opened, at this state when i click outside of edit text, keyboard is still open and edit text has still focus. Do I also need to set sth in xml for edit text? – sinan Dec 08 '19 at 10:16
  • 1
    hmmm that is very odd. It should work. To clarify what I ussually do is make an Extenstions.kt file under a uitl folder. I place the above 3 functions in the Extensions.kt file and then with the public hideKeyBoardOnPressAway() it should work. If you wanted to try making the edit text focusable in XML that may change things. – Eli Dangerfield Dec 08 '19 at 17:28
  • I tried that as well but still no success. Thanks anyway for your effort – sinan Dec 10 '19 at 22:01