Skip to content

Instantly share code, notes, and snippets.

@iosandroiddev
Last active June 26, 2021 08:23
Show Gist options
  • Save iosandroiddev/7cf1cb6821a9550250f27c0d794c241b to your computer and use it in GitHub Desktop.
Save iosandroiddev/7cf1cb6821a9550250f27c0d794c241b to your computer and use it in GitHub Desktop.
Extensions for Keyboard hiding
@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