Skip to content

Instantly share code, notes, and snippets.

@Skyyo
Last active August 16, 2020 11:17
Show Gist options
  • Save Skyyo/a0436094ef0f7d87b884e45435d3d3c6 to your computer and use it in GitHub Desktop.
Save Skyyo/a0436094ef0f7d87b884e45435d3d3c6 to your computer and use it in GitHub Desktop.
#extensions
package com.skyyo.ext
import android.app.Activity
import android.content.Context
import android.view.inputmethod.InputMethodManager
import androidx.fragment.app.Fragment
fun Fragment?.hideKeyboard() =
(this?.activity?.getSystemService(Activity.INPUT_METHOD_SERVICE) as InputMethodManager?)?.let {
if (it.isActive) it.hideSoftInputFromWindow(this@hideKeyboard?.view?.windowToken, 0)
}
fun Fragment?.showKeyboard() =
(this?.activity?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager).toggleSoftInput(
InputMethodManager.SHOW_IMPLICIT, InputMethodManager.HIDE_IMPLICIT_ONLY
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment