Last active
November 16, 2021 16:25
-
-
Save zakrodionov/fbe5ca9c8e126dcbc47efc3dc78c1e4d to your computer and use it in GitHub Desktop.
Hide keyboard on outside touch (3 popular methods) //Best on top
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
https://stackoverflow.com/questions/4165414/how-to-hide-soft-keyboard-on-android-after-clicking-outside-edittext |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun Activity.hideKeyboardOnClickOutsideEditText(view: View) { | |
// Set up touch listener for non-text box views to hide keyboard. | |
var previousAction = 0 | |
val onTouchListener = View.OnTouchListener { v, event -> | |
if (currentFocus != null | |
&& event.action != MotionEvent.ACTION_DOWN | |
&& event.action != MotionEvent.ACTION_MOVE | |
&& previousAction != MotionEvent.ACTION_MOVE | |
) { | |
currentFocus?.clearFocus() | |
v?.hideKeyboard() | |
} | |
previousAction = event.action | |
false | |
} | |
if (view !is EditText) { | |
view.setOnTouchListener(onTouchListener) | |
} | |
//If a layout container, iterate over children and seed recursion. | |
if (view is ViewGroup) { | |
for (i in 0 until view.childCount) { | |
val innerView = view.getChildAt(i) | |
hideKeyboardOnClickOutsideEditText(innerView) | |
} | |
} | |
} | |
//use currentFocus?.clearFocus() or | |
//in parent viewgroup layout.xml | |
android:clickable="true" | |
android:focusable="true" | |
android:focusableInTouchMode="true" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun hideKeyboardOutsideTouchEditText(view: View) { | |
// Set up touch listener for non-text box views to hide keyboard. | |
val onTouchListener = View.OnFocusChangeListener { v, hasFocus -> | |
tryOrIgnore { | |
if (!hasFocus) { | |
v?.hideKeyboard() | |
} | |
} | |
} | |
if (view is EditText) { | |
view.setOnFocusChangeListener(onTouchListener) | |
} | |
//If a layout container, iterate over children and seed recursion. | |
if (view is ViewGroup) { | |
for (i in 0 until view.childCount) { | |
val innerView = view.getChildAt(i) | |
hideKeyboardOutsideTouchEditText(innerView) | |
} | |
} | |
} | |
or | |
fun View.hideKeyboardOutsideTouch() { | |
setOnFocusChangeListener { v, hasFocus -> | |
if (!hasFocus) { | |
hideKeyboard() | |
} | |
} | |
} | |
etAccName.hideKeyboardOutsideTouch() | |
etDeviceName.hideKeyboardOutsideTouch() | |
in parent viewgroup layout.xml | |
android:clickable="true" | |
android:focusable="true" | |
android:focusableInTouchMode="true" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Activity.kt | |
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean { | |
if (currentFocus != null) { | |
val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager | |
imm.hideSoftInputFromWindow(currentFocus!!.windowToken, 0) | |
} | |
return super.dispatchTouchEvent(ev) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment