Home » Android » Android – show keyboard programmatically

Android – show keyboard programmatically

Posted by: admin April 23, 2020 Leave a comment

Questions:

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);
}
How to&Answers:

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);

Answer:

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);


}

Answer:

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);
    }