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
  • why do you trying to find some view? pass View v as a parameter to your hideKeyboard method, v.getContext() will give you the right context and that would be method with 2 lines of code Commented May 28, 2017 at 11:28
  • Have you initialized or assigned ctx with a Context object? Commented May 28, 2017 at 11:29
  • @ViktorYakunin Please elaborate your answer with code... Commented May 28, 2017 at 11:32
  • Possible duplicate of Close/hide the Android Soft Keyboard Commented May 28, 2017 at 11:45

5 Answers 5

12

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() } }) 
Sign up to request clarification or add additional context in comments.

1 Comment

This doesn´t work with fragments, on a mate 20 pro device
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); } 

Comments

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

Comments

0

Hide it using the window token from your fragment view

For Kotlin -

if (activity != null){ // Hide soft keyboard val imm = activity!!.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager imm.hideSoftInputFromWindow(requireView().windowToken, 0) } 

Comments

-1

Try this:

View view = this.getCurrentFocus(); if (view != null) { InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(view.getWindowToken(), 0); } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.