Search code examples
androidandroid-jetpack-composeandroid-compose-textfieldjetpack-compose-modalbottomsheetandroid-modalbottomsheetlayout

Jetpack Compose TextField in ModalBottomSheetLayout, keyboard won't hide automatically


If hide the bottom sheet by drag gesture then keyboard won't hide automatically

enter image description here

enter image description here

How can I fix it?


Solution

  • Since you haven't shared any code here, this is how I would have handle this issue:

    Use the Disposable Effect and onDispose handle the keyboard hiding

    Sample Code:

    ...
    val keyboardController = LocalSoftwareKeyboardController.current
    
      DisposableEffect(key1 = modalBottomSheetState.isVisible, effect = {
            onDispose {
                keyboardController?.hide()
            }
        })
    ...
    

    The LocalSoftwareKeyboardController is marked as Experimental so you can also do the following:

    ...
     val focusManager = LocalFocusManager.current
    
      DisposableEffect(key1 = modalBottomSheetState.isVisible, effect = {
            onDispose {
                 focusManager.clearFocus()
            }
        })
    ...