android-ktx icon indicating copy to clipboard operation
android-ktx copied to clipboard

Feature suggestion: Show/hide Soft Input

Open VedavyasBhat opened this issue 7 years ago • 2 comments

Context.hideSip(view: View)
Context.showSip(view: View)

Activity.hideSip()
Activity.showSip()

VedavyasBhat avatar Feb 06 '18 08:02 VedavyasBhat

I usually have View.showKeyboard() instead of Context.showKeyboard(view) in my projects

Edward608 avatar Feb 06 '18 09:02 Edward608

That's what I use

fun View.focusAndshowKeyboard() {
    requestFocusFromTouch()
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

fun View.showKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

fun View.hideKeyboard() {
    val imm = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(getWindowToken(), 0)
}

fun Activity.showKeyboard() {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY)
}

fun Activity.hideKeyboard() {
    val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0)
}

vdubedout avatar Feb 06 '18 17:02 vdubedout