Last active
June 26, 2021 08:23
-
-
Save iosandroiddev/7cf1cb6821a9550250f27c0d794c241b to your computer and use it in GitHub Desktop.
Extensions for Keyboard hiding
This file contains 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
@file:JvmName("KeyBoardExt") | |
import android.app.Activity | |
import android.content.Context | |
import android.view.View | |
import android.view.inputmethod.InputMethodManager | |
import androidx.appcompat.app.AppCompatActivity | |
import androidx.fragment.app.Fragment | |
fun Fragment.hideKeyboard() { | |
view?.let { activity?.hideKeyboard(it) } | |
} | |
fun AppCompatActivity.hideKeyboard() { | |
hideKeyboard(currentFocus ?: View(this)) | |
} | |
fun Context.hideKeyboard(view: View) { | |
val inputMethodManager = getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager | |
inputMethodManager.hideSoftInputFromWindow(view.windowToken, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment