how to hide keyboard after typing in EditText in android?

AndroidAndroid Edittext

Android Problem Overview


I have a EditText and button aligned to parent's bottom.

When I enter text in it and press the button to save data, the virtual keyboard does not disappear.

Can any one guide me how to hide the keyboard?

Android Solutions


Solution 1 - Android

You might also want to define the imeOptions within the EditText. This way, the keyboard will go away once you press on Done:

<EditText
    android:id="@+id/editText1"
    android:inputType="text"
    android:imeOptions="actionDone"/>

Solution 2 - Android

This should work.

InputMethodManager inputManager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); 
inputManager.hideSoftInputFromWindow(this.getCurrentFocus().getWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS); 

Just make sure that this.getCurrentFocus() does not return null, which it would if nothing has focus.

Solution 3 - Android

   mEtNumber.setOnEditorActionListener(new TextView.OnEditorActionListener() {

            @Override
            public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
                if (actionId == EditorInfo.IME_ACTION_DONE) {
                    // do something, e.g. set your TextView here via .setText()
                    InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
                    imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
                    return true;
                }
                return false;
            }
        });

and in xml

  android:imeOptions="actionDone"

Solution 4 - Android

getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);

Solution 5 - Android

I did not see anyone using this method:

editText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View view, boolean focused) {
        InputMethodManager keyboard = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        if (focused)
            keyboard.showSoftInput(editText, 0);
        else
            keyboard.hideSoftInputFromWindow(editText.getWindowToken(), 0);
    }
});

And then just request focus to the editText:

editText.requestFocus();

Solution 6 - Android

Solution included in the EditText action listenner:

public void onCreate(Bundle savedInstanceState) {
    ...
    ...
    edittext = (EditText) findViewById(R.id.EditText01);
    edittext.setOnEditorActionListener(new OnEditorActionListener() {
        public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
            if (event != null&& (event.getKeyCode() == KeyEvent.KEYCODE_ENTER)) {
                InputMethodManager in = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                in.hideSoftInputFromWindow(edittext.getApplicationWindowToken(),InputMethodManager.HIDE_NOT_ALWAYS);
            }
            return false;
        }
    });
    ...
    ...
}

Solution 7 - Android

I found this because my EditText wasn't automatically getting dismissed on enter.

This was my original code.

editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if ( (actionId == EditorInfo.IME_ACTION_DONE) || ((event.getKeyCode() == KeyEvent.KEYCODE_ENTER) && (event.getAction() == KeyEvent.ACTION_DOWN ))) {

            // Do stuff when user presses enter

            return true;

        }

        return false;
    }
});

I solved it by removing the line

return true;

after doing stuff when user presses enter.

Hope this helps someone.

Solution 8 - Android

Just write down these two lines of code where enter option will work.

InputMethodManager inputManager = (InputMethodManager) getSystemService(Activity.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(view.getWindowToken(), 0);

Solution 9 - Android

Been struggling with this for the past days and found a solution that works really well. The soft keyboard is hidden when a touch is done anywhere outside the EditText.

Code posted here: https://stackoverflow.com/questions/4005728/hide-default-keyboard-on-click-in-android/7241790#7241790

Solution 10 - Android

You can see marked answer on top. But i used getDialog().getCurrentFocus() and working well. I post this answer cause i cant type "this" in my oncreatedialog.

So this is my answer. If you tried marked answer and not worked , you can simply try this:

InputMethodManager inputManager = (InputMethodManager) getActivity().getApplicationContext().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getDialog().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Solution 11 - Android

you can create a singleton class for call easily like this:

public class KeyboardUtils {

    private static KeyboardUtils instance;
    private InputMethodManager inputMethodManager;

    private KeyboardUtils() {
    }

    public static KeyboardUtils getInstance() {
        if (instance == null)
            instance = new KeyboardUtils();
        return instance;
    }

    private InputMethodManager getInputMethodManager() {
        if (inputMethodManager == null)
            inputMethodManager = (InputMethodManager) Application.getInstance().getSystemService(Activity.INPUT_METHOD_SERVICE);
        return inputMethodManager;
    }

    @SuppressWarnings("ConstantConditions")
    public void hide(final Activity activity) {
        new Handler().post(new Runnable() {
            @Override
            public void run() {
                try {
                    getInputMethodManager().hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
                } catch (NullPointerException e) {
                    e.printStackTrace();
                }
            }
        });
    }
}

so, after can call in the activity how the next form:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity);
        KeyboardUtils.getInstance().hide(this);
    }

}

Solution 12 - Android

I use this method to remove keyboard from edit text:

 public static void hideKeyboard(Activity activity, IBinder binder) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (binder != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(binder, 0);//HIDE_NOT_ALWAYS
            inputManager.showSoftInputFromInputMethod(binder, 0);
        }
    }
}

And this method to remove keyboard from activity (not work in some cases - for example, when edittext, to wich is binded keyboard, lost focus, it won't work. But for other situations, it works great, and you do not have to care about element that holds the keyboard).

 public static void hideKeyboard(Activity activity) {
    if (activity != null) {
        InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
        if (activity.getCurrentFocus() != null && inputManager != null) {
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), 0);
            inputManager.showSoftInputFromInputMethod(activity.getCurrentFocus().getWindowToken(), 0);
        }
    }
}

Solution 13 - Android

int klavStat = 1; // for keyboard soft/hide button
int inType;  // to remeber your default keybort Type

editor - is EditText field

/// metod for onclick button ///
public void keyboard(View view) {
    if (klavStat == 1) {
        klavStat = 0;
        
        inType = editor.getInputType();
        
        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.toggleSoftInput(0, InputMethodManager.HIDE_NOT_ALWAYS);
     

        editor.setInputType(InputType.TYPE_NULL);

        editor.setTextIsSelectable(true);
    } else {
        klavStat = 1;


        InputMethodManager imm = (InputMethodManager)
                getSystemService(Context.INPUT_METHOD_SERVICE);

        imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

        editor.setInputType(inType);
    }
}

If you have another EditText Field, you need to watch for focus change.

Solution 14 - Android

In my case, in order to hide the keyboard when pressing the "send button", I used the accepted answer but changed context to getApplication and added getWindow().

InputMethodManager inputManager = (InputMethodManager) getApplication().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(getWindow().getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);

Solution 15 - Android

editText.setInputType(InputType.TYPE_NULL);

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionUMAR-MOBITSOLUTIONSView Question on Stackoverflow
Solution 1 - AndroidangelrhView Answer on Stackoverflow
Solution 2 - AndroidRyan AlfordView Answer on Stackoverflow
Solution 3 - AndroidAnkita ChopraView Answer on Stackoverflow
Solution 4 - Androidsumit pandeyView Answer on Stackoverflow
Solution 5 - AndroidOfirView Answer on Stackoverflow
Solution 6 - AndroidyohmView Answer on Stackoverflow
Solution 7 - AndroidIsaiahJView Answer on Stackoverflow
Solution 8 - AndroidShahadat HossainView Answer on Stackoverflow
Solution 9 - AndroidDanielView Answer on Stackoverflow
Solution 10 - AndroidYasin KaçmazView Answer on Stackoverflow
Solution 11 - AndroidAlex ZaraosView Answer on Stackoverflow
Solution 12 - AndroidAnton KizemaView Answer on Stackoverflow
Solution 13 - AndroidD.DzugoView Answer on Stackoverflow
Solution 14 - AndroidLeo SView Answer on Stackoverflow
Solution 15 - AndroidTOLIN THOMASView Answer on Stackoverflow