Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to hide soft keyboard in fragments?

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"

like image 266
Abdulmateen Ch. Avatar asked May 28 '17 11:05

Abdulmateen Ch.


People also ask

How do I hide my soft keyboard?

Hiding the Soft Keyboard Programmatically You can force Android to hide the virtual keyboard using the InputMethodManager, calling hideSoftInputFromWindow, passing in the token of the window containing your edit field. This will force the keyboard to be hidden in all situations.

How do I prevent the soft keyboard from pushing my view up in fragment?

Setting android:isScrollContainer = "false" inside the ScrollView worked for me. According to the documentation, settings "isScrollContainer" to true means that the scroll view can be resized to shrink its overall window so that there is space for an input method.

How do I hide the buttons on my keyboard?

To hide keyboard, use the following code. InputMethodManager inputMethodManager = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE); inputMethodManager. hideSoftInputFromWindow(v. getApplicationWindowToken(),0);


1 Answers

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()
  }
})
like image 145
Aniruddh Parihar Avatar answered Sep 24 '22 14:09

Aniruddh Parihar