0

I have some Fragments with EditTexts. When I write text in EditText on the first fragment I open next fragment. How can I hide the keyboard?

I am doing this:

public static void hideSoftKeyboard(Activity activity) {
        InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
    }

but if i return on first fragment and again open next fragment focus lose. and i have error.

I found

EditText myEditText = (EditText) findViewById(R.id.myEditText);  
InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

But I have many fields on the fragment and many fragments with fields. I don't want duplicate code.

My error is:

java.lang.NullPointerException
            at com.skip.client.customer.utils.Utils.hideSoftKeyboard(Utils.java:13)
            at com.skip.client.customer.fragments.auth_fragments.SignUpChooseTypeAuthFragment$EventHandler.onValidationSucceeded(SignUpChooseTypeAuthFragment.java:102)
            at com.mobsandgeeks.saripaar.Validator.triggerValidationListenerCallback(Validator.java:673)
            at com.mobsandgeeks.saripaar.Validator.validateFieldsWithCallbackTill(Validator.java:651)
            at com.mobsandgeeks.saripaar.Validator.validateUnorderedFieldsWithCallbackTill(Validator.java:633)
            at com.mobsandgeeks.saripaar.Validator.validate(Validator.java:313)
            at com.mobsandgeeks.saripaar.Validator.validate(Validator.java:274)
            at com.skip.client.customer.fragments.auth_fragments.SignUpChooseTypeAuthFragment$EventHandler.onClick(SignUpChooseTypeAuthFragment.java:89)
            at android.view.View.performClick(View.java:4637)
            at android.view.View$PerformClick.run(View.java:19422)
            at android.os.Handler.handleCallback(Handler.java:733)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5586)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1268)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1084)
            at dalvik.system.NativeStart.main(Native Method)
Ciaran Donoghue
  • 720
  • 2
  • 15
  • 44
ip696
  • 4,826
  • 7
  • 45
  • 94

5 Answers5

1

You could pass a View to your hideSoftKeyboard method and get a context from a View's getContext().

public static void hideSoftKeyboard(View view) {
    InputMethodManager inputMethodManager =
        (InputMethodManager) view.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
    inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
  }

Need to say passing an Activity around could be a bad idea because this may entail memory leaks which are hard to debug and fix.

Eugene
  • 55,777
  • 85
  • 212
  • 324
0

try

    public static void hideSoftKeyboard(Activity activity, View view) {
          if(activity!= null && !activity.isFinish() && view != null) 
          {
             InputMethodManager inputMethodManager = (InputMethodManager)  activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
             inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), 0);
          }
    }
Thanh Le
  • 754
  • 3
  • 13
0

Use this code

private void hideSoftKeyBoard(ScrollView view){
    InputMethodManager inputManager = (InputMethodManager)getActivity().getSystemService(Activity.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
}

This is working for me.

Rahul Patidar
  • 386
  • 2
  • 12
0

Try following working for me

public static void hideSoftKeyboard(Activity activity) {
    if (null != activity.getWindow()){
    activity.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
  }
}
Narendra
  • 603
  • 1
  • 12
  • 27
0
 public static void hideKeyboard(Activity activity) {
    // Check if no view has focus:
    View view = activity.getCurrentFocus();
    if (view != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        inputManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
    }
}
SERG
  • 3,383
  • 7
  • 34
  • 67