5

i want to hide keyboard in fragments in android.Because once it displays it remain visible in all fragments.I try this method

    public static void hideKeyboard(Context ctx) {
    InputMethodManager inputManager = (InputMethodManager) ctx
            .getSystemService(Context.INPUT_METHOD_SERVICE);

    // check if no view has focus:
    View v = ((Activity) ctx).getCurrentFocus();
    if (v == null)
        return;

    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}

and call this method on button click

signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                hideKeyboard(ctx);
                login();


        }
    });

but this give error "java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference"

4 Answers4

10

For Java

try this one

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

to call this just pass below code from your onclick of button

signIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
               hideSoftKeyboard(getActivity());
                login();


        }
    });

For Kotlin

fun hideSoftKeyboard(activity:Activity) {
  if (activity.getCurrentFocus() == null){
    return
  }
  val inputMethodManager = activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
  inputMethodManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0)
}

pass below code from your onclick of button

signIn.setOnClickListener(object:View.OnClickListener() {
  fun onClick(v:View) {
    hideSoftKeyboard(getActivity())
    login()
  }
})
Aniruddh Parihar
  • 2,524
  • 1
  • 16
  • 32
3
signIn.setOnClickListener(new View.OnClickListener() {
   @Override
   public void onClick(View v) {
       hideKeyboard(v);
       login();
   }
});

And the hide keyboard method in some Utility class

public static void hideKeyboard(@NonNull View v) {
    InputMethodManager inputManager = (InputMethodManager) v.getContext()
            .getSystemService(Context.INPUT_METHOD_SERVICE);
    inputManager.hideSoftInputFromWindow(v.getWindowToken(), 0);
}
Viktor Yakunin
  • 2,777
  • 3
  • 21
  • 34
0

Use this for kotlin users create a file and add this code

fun hideSoftKeyboard(activity: Activity) {
    if (activity.currentFocus == null) {
        return
    }
    val inputMethodManager =
        activity.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(activity.currentFocus!!.windowToken, 0)
}

Now call this method anywhere using

hideSoftKeyboard(requireActivity())

Happy coding..NB for kotlin

Moses Altruism
  • 138
  • 1
  • 6
-1

Try this:

View view = this.getCurrentFocus();
if (view != null) {
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
β.εηοιτ.βε
  • 16,236
  • 11
  • 41
  • 53