Closing Soft Keyboard in Fragment

hide soft keyboard android after clicking outside edittext in fragment
android hide keyboard onclick in fragment
fragment lifecycle
hide keyboard in fragment android kotlin
how to hide the keyboard in fragment android
open soft keyboard in fragment
show keyboard in fragment android
hide keyboard when changing fragment

I found a number of answers to this question but none of them are working for me. I have an Edit text in my Fragment, which gets launched when the application starts. When this Fragment opens, the soft keyboard pops up as well. How do I prevent that from happening? This is what I have in my onCreateView method in my Fragment....

        try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
        imm.hideSoftInputFromWindow(userName.getWindowToken(), 0);
    }catch(Exception e) {

Try this in onCreateView or onActivityCreated.


How to hide the soft keyboard from inside a fragment?, I tried to use this in both fragments, in the onCreate method. I also tried to hide the soft keyboard in the layout: android:windowSoftInputMode="� How do I close or hide the soft keyboard in Android? Simple question. INSANELY complex answer. To help clarify the madness that follows, I’d like to begin by apologizing on behalf of all Android

My recently project I use the code as follow to hide the keyboard layout, maybe you can try it.(I learn it from the source code of Wordpress-android)

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_template_add_doc, container, false);
    //hide the keyboard if it is visible
    InputMethodManager imm = (InputMethodManager) getActivity()
    imm.hideSoftInputFromWindow(view.getApplicationWindowToken(), 0);
    return view;

Hide keyboard when navigating from a fragment to another, Hiding the keyboard from a Fragment. But suppose you want to hide the keyboard from an EditText hosted in a DialogFragment ? You can't use� but it can not show soft keyboard at all in my fragment. I can not put EditText to Activity because it need to refactor a lot of code.

Try the below logics to hide the keyboard from opening automatically.

Try to place your edit text in a separate linearlayout and set android:focusableInTouchMode="true". This will automatically avoids the keyboard opening automatically.

     android:id = "@+id/layout"
     android:layout_width = "wrap_content"
     android:layout_height = "wrap_content"
     android:focusable = "true"
     android:focusableInTouchMode = "true">

       android:id = "@+id/edit_text"
       android:layout_width = "match_content"
       android:layout_height = "wrap_content"/>

or if the above fails, hide Programmatically by using the code below. Write it as a separate function and call it in the code.

call this method in your fragment after view is created as below.

  public void onViewCreated(View view, Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);

public void hideKeyboard() {
    InputMethodManager inputMethodManager = (InputMethodManager) activity

                    .getWindowToken(), 0);
  } // hideKeyboard

Good Luck..!

Close/Hide the Soft Keyboard in Android, If the keyboard is showing from an EditText hosted in a DialogFragment, use this method instead: hide_keyboard(get_activity()); //won't work This� Browse All Articles > How to Close/Hide the Android Soft Keyboard Android supports both an on-screen keyboard — soft keyboard — and an attached hardware keyboard. Generally, the soft keyboard is supposed to appear when there is a text field, and disappears when the field is out of focus.

This worked for me,try this way

    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {


    public static void hideKeyboard( Context context ) {

            try {
                InputMethodManager inputManager = ( InputMethodManager ) context.getSystemService( Context.INPUT_METHOD_SERVICE );

                View view = ( (Activity) context ).getCurrentFocus();
                if ( view != null ) {
                    inputManager.hideSoftInputFromWindow( view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS );
            } catch ( Exception e ) {

How to Close/Hide the Android Soft Keyboard, hide soft keyboard android after clicking outside edittext in fragment If you want to close the soft keyboard during a unit or functional test, you can do so by� Show/hide Android softkeyboard on fragment replace (6) I have an Activity with a fragment. Let's say a list fragment with a list of things. Now I want to let the user add a thing, so I use the FragmentManager to replace the list fragment with an insert fragment which has an EditText.

You can also add this line in AndroidManifest.xml to your fragment activity.

line to add:- android:windowSoftInputMode="stateHidden|adjustResize"

see code snippet below:-

<activity android:name=".activity.FragmentActivity"

How to hide the soft keyboard from inside a fragment?, Tips for working with the soft keyboard on Android. mechanism prevents one application from force-closing the keyboard opened by another application. Retrieving the token if the view is hosted by the fragment. var windowToken: IBinder? It works fine but how to close this .. I have tried all the solution . In my scenario I start a fragment and show the keyboard and press the home button .. my app goes in background but keyboard still there on tab/phone screen .. it seems weird cause when app is closed or in background why keyboard is not closing or hiding , I was trying to write some code on pause of fragment but did not help.

The Soft Keyboard on Android, I got a edittext in a dialogfragment, and the keyboard pops up when i it comes in SetSoftInputMode hide virtual keyboard � dialogfragment. 0� The Android system shows an on-screen keyboard—known as a soft input method—when a text field in your UI receives focus.To provide the best user experience, you can specify characteristics about the type of input you expect (such as whether it's a phone number or email address) and how the input method should behave (such as whether it performs auto-correct for spelling mistakes).

How to hide keyboard in DialogFragment when tapping anywhere , When the DialogFragment is created the soft keyboard is shown immediately and the EditText gains The keyboard will remain visible after the dialog closes. This is an introductory tutorial for my Android Programming Class where we learn to work with an EditText field within a Fragment.

Dismissing a DialogFragment when the soft keyboard is hidden , The Android system shows an on-screen keyboard, known as a soft input method , when a text field in your UI receives focus. To provide the best user� Close/hide the Android Soft Keyboard in MvxFragment. I think, do you include the hide keyboard to activity, no to Fragment. Also include this in onCreate method.

  • can you please post your XML?