Search code examples
androidandroid-edittextkeyboardandroid-keypadwindow-soft-input-mode

How to always show soft keyboard and do not let it be closed?


I want to know 2 things

  1. how to always show soft keyboard and do not let it be closed ( even when back or OK button was pressed ) ?

  2. and how can I get an input from it?

I have already tried this code:

    EditText yourEditText= (EditText) findViewById(R.id.ed);
    InputMethodManager imm = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);

with these variants:

  1. imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);
  2. imm.toggleSoftInputFromWindow( yourEditText.getApplicationWindowToken(), InputMethodManager.SHOW_FORCED, 0);
  3. imm.showSoftInput(yourEditText, InputMethodManager.SHOW_IMPLICIT);

Solution

  • Use android:windowSoftInputMode="stateAlwaysVisible" in to AndroidManifest.xml file

    Like this:

    <activity android:name=".YourActivity"
    android:label="@string/app_name"
    android:windowSoftInputMode="stateAlwaysVisible" />  // OR stateVisible
    

    If that activity having EditText so when ever Activity will start your Keyboard automatically open

    If you want to still open Keyboad after use done any operation then do this via programetically

    InputMethodManager imm =
        (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.toggleSoftInputFromWindow(
        linearLayout.getApplicationWindowToken(),
        InputMethodManager.SHOW_FORCED, 0);
    

    OR

    To Show Soft Keyboard

    InputMethodManager imm = (InputMethodManager)
         getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(EDITABLE_VIEW,
         InputMethodManager.SHOW_IMPLICIT);
    

    OR

    EDITABLE_VIEW can be any view which has focus on screen like

    mEditText = (EditText) findViewById(R.id.editText);
    InputMethodManager imm = (InputMethodManager)
         getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mEditText ,
         InputMethodManager.SHOW_IMPLICIT);
    

    OR

    ((InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE)).showSoftInputFromInputMethod(editText.getWindowToken(), 0);
    

    Documentation