48

I have an EditText field which is disabled at the beginning.

I would like to set it to enabled, put the cursor on it and the keyboard should be visible.

I tried the following code and all works - only the keyboard will not be shown.

@Override
protected void onCreate(Bundle savedInstanceState{
    editText.setEnabled(true);
    editText.requestFocus();
    getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(editText, InputMethodManager.SHOW_IMPLICIT);
}
0xCursor
  • 2,242
  • 4
  • 15
  • 33
Trombone0904
  • 4,132
  • 8
  • 51
  • 104

3 Answers3

122

This is possibly a deprecated solution now.

For hiding keyboard:

InputMethodManager imm = (InputMethodManager)getSystemService(
  Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

For Showing keyboard:

InputMethodManager imm = (InputMethodManager)   getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
Nabeel K
  • 5,938
  • 11
  • 38
  • 68
17

try my showKeyboard method

 public static void showKeyboard(EditText mEtSearch, Context context) {
    mEtSearch.requestFocus();
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Activity.INPUT_METHOD_SERVICE);
    imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
}

public static void hideSoftKeyboard(EditText mEtSearch, Context context) {
    mEtSearch.clearFocus();
    InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mEtSearch.getWindowToken(), 0);


}
hugerde
  • 919
  • 1
  • 12
  • 27
4

You can use this method for showing keyboard forcefully after calling requestFocus() on EditText.

public static void showKeyboard(FragmentActivity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.toggleSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.SHOW_FORCED, 0);
    }
vijaypalod
  • 398
  • 4
  • 15