0

I want to clear focus of TextInputEditTexts when the user taps outside of them. In order to do that I use the code from this answer. Because that does not work in dialogs I added the following code in my bottom sheet, as described here:

class MySheet(): BottomSheetDialogFragment() {
    ...
    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {

        return object : Dialog(requireActivity(), theme) {
            override fun dispatchTouchEvent(event: MotionEvent): Boolean {
                if (event.action == MotionEvent.ACTION_DOWN) {
                    val v = currentFocus
                    if (v is TextInputEditText) {
                        val outRect = Rect()
                        v.getGlobalVisibleRect(outRect)
                        if (!outRect.contains(event.rawX.toInt(), event.rawY.toInt())) {
                            v.clearFocus()
                            val imm: InputMethodManager =
                                context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
                            imm.hideSoftInputFromWindow(v.getWindowToken(), 0)
                        }
                    }
                }
                return super.dispatchTouchEvent(event)
            }
        }
    }
}

But now, the bottom sheet is displayed as a normal dialog with margins on all sides. How can I force the normal behaviour of a modal bottom sheet?

0 Answers0