Home > Software design >  Android studio Fragment view as a parameter?
Android studio Fragment view as a parameter?

Time:08-22

I'm writing an application and i have a certain function, which takes view: View as a parameter:

private fun closeKeyboard(view: View) {
    val imm = view.context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(view.windowToken, 0)
}

The entire aplication is written with fragments. This function is meant to hide the keyboard, when submit button is clicked. However, i don't know how to pass Fragment's view as a parameter when i try to use the function:

private fun onSubmitNumber(){
    val userNumber = binding?.textInputEditText?.text.toString()

    closeKeyboard()

How to get a View type parameter from a fragment?

CodePudding user response:

You can simply use any view in your fragment:

private fun onSubmitNumber(){
    val userNumber = binding?.textInputEditText?.text.toString()
    closeKeyboard(binding?.root)
}
  • Related