-
Notifications
You must be signed in to change notification settings - Fork 0
/
hide_keyboard.kt
42 lines (32 loc) · 1.64 KB
/
hide_keyboard.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// when touch areas out of keyboard, hide keyboard feature
//in fragment
class HideKeyboardFragment() : Fragment() {
private lateinit var viewDataBinding: FragmentHideKeyboard
...
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?): View? { //This code must be in OnCreateView
viewDataBinding.layoutId.setOnTouchListener(object : View.OnTouchListener { //layoutId is a whole layout which covering the another layout(First create layout ex:ConstraintLayout ...etc)
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
val inputMethodManager = activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(activity?.currentFocus?.windowToken, 0)
return false
}
})
}
...
}
//in activity
class HideKeyboardActivity : AppCompatActivity() {
private lateinit var viewDataBinding: ActivityHideKeyboard
...
override fun onCreate(savedInstanceState: Bundle?) {//This code must be in onCreate
viewDataBinding.layoutId.setOnTouchListener(object : View.OnTouchListener { //layoutId is a whole layout which covering the another layout(First create layout ex:ConstraintLayout ...etc)
override fun onTouch(v: View?, event: MotionEvent?): Boolean {
val inputMethodManager = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
inputMethodManager.hideSoftInputFromWindow(currentFocus?.windowToken, 0)
return false
}
})
}
...
}