0

I am using a FrameLayout to show an EditText and a ListView (with checkboxes) alternately. When showing an EditText, I would like the soft keyboard to be shown. And when showing the ListView, I would like the soft keyboard to be hidden. Now usually a focus is needed in order to hide the soft keyboard. When my ListView gets shown, then getCurrentFocus() returns null. Is there a way to hide the soft keyboard, without having a focus?

I am showing the soft keyboard like that:

public static void requestFocusAndMoveCursorToTheEndAndShowKeyboard(final EditText editTextParam, final Activity activityParam) {
    if (editTextParam == null) {
        return;
    }

    if (editTextParam.requestFocus()) {
        editTextParam.setSelection(editTextParam.getText().length()); // move Cursor to the end of the EditText
        InputMethodManager imm = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, InputMethodManager.HIDE_IMPLICIT_ONLY);
    }
}

And I am trying to hide the soft keyboard like that:

public static void hideSoftInputKeyboardFromWindow(Activity activityParam) {
    if (activityParam == null) {
        return;
    }

    View view = activityParam.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputMethodManager = (InputMethodManager) activityParam.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
Jørgen R
  • 10,568
  • 7
  • 42
  • 59
dev456
  • 11
  • 2

6 Answers6

2

Make use of .clearFocus(); on edittext when you don't want a focus on it.

Fahim
  • 12,198
  • 5
  • 39
  • 57
1

In your AndroidMenifest.xml add this:

<activity android:name="com.your.package.ActivityName"
      android:windowSoftInputMode="stateHidden"  />
Mohammad Arman
  • 7,020
  • 2
  • 36
  • 50
0

Try this: Write following method to youractivity or to your utility class

/**
 * Hide soft keypad
 * 
 */


public static void hideKeyboard(Activity activity, View v) {
        InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
    }
Namrata
  • 1,683
  • 1
  • 17
  • 28
0

In your hideSoftInputKeyboardFromWindow method, try:

inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

instead of

inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Edit: ok same answer as Dorami

Groco
  • 1,301
  • 15
  • 19
0

How do you show them alternately? Using different fragments? Or do you simply inflate different layouts? Provide more details with your complete code

priyank
  • 2,651
  • 4
  • 24
  • 36
0

Thank you for your answers. Finally I got it solved using a View.OnFocusChangeListener for the EditText, like described here:

Hide soft keyboard on losing focus

Community
  • 1
  • 1
dev456
  • 11
  • 2