Android:Hide keyboard after button click
Asked Answered
S

10

38

I need to hide the android keyboard after a button click.

I have seen many examples of how to do this, however, they all appear to use a specific editText object.

e.g.

InputMethodManager imm = (InputMethodManager)getSystemService(
      Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);

My problem is that I am building the screen dynamically, thus there could be mane edit text fields. Is there a way the keyboard can be hidden without me having to specify which editText object I am hiding it for.

Sills answered 27/11, 2012 at 21:7 Comment(1)
You can hide it for the whole Activity, this sums it up nicely: #7790014Jegar
R
63

You could instead set it to your layout, ie:

LinearLayout mainLayout;

// Get your layout set up, this is just an example
mainLayout = (LinearLayout)findViewById(R.id.myLinearLayout);

// Then just use the following:
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mainLayout.getWindowToken(), 0);

This is an example assuming that your layout will be created regardless of how many EditText objects (or other objects) are placed on it.

Edit: Also, something I find very useful is to make sure that the keyboard is hidden when an activity first launches (ie: if an EditText is the first thing focused). To do that, I put this in onCreate() method of Activity:

 this.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Regazzi answered 27/11, 2012 at 21:17 Comment(1)
Note that it also works for other view parts, e.g. an EditText :) Then you would change the following line: imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);Disinterested
P
57

Dont forget to use try catch blog because in case when your keyboard not open and if you use key key board hide code app will crash

try {
    InputMethodManager imm = (InputMethodManager)getSystemService(INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
} catch (Exception e) {
    // TODO: handle exception
}
Proctology answered 1/12, 2014 at 12:36 Comment(2)
by far the most simple and elegant solutionFactious
Most ellegant, generic and simple solution, i love itBrandeebranden
B
15

You can hide the keyboard using the following code, probably on the Button click Event :

//================ Hide Virtual Key Board When  Clicking==================//

InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow("Your Button/EditText Object".getWindowToken(), InputMethodManager.RESULT_UNCHANGED_SHOWN);

//======== Hide Virtual Keyboard =====================//
Bronchi answered 24/7, 2015 at 8:25 Comment(0)
O
10
edittext.onEditorAction(EditorInfo.IME_ACTION_DONE);
Outstation answered 9/11, 2017 at 9:53 Comment(1)
Underrated answerKidd
B
9

If the problem is on an activity simply the following will work:

    try {
        InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

else if the code is required in a fragment do the following

    try {
        InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(getActivity().getCurrentFocus().getWindowToken(), 0);
    } catch (Exception e) {
        // TODO: handle exception
    }

This will handle the hiding of keyboard on a button click or any other event as deemed specific when written within the event block.

Bancroft answered 10/2, 2017 at 9:29 Comment(0)
S
4

For the record and based in the answers of @burmat and @Prashant Maheshwari Andro

Let's say that you call the click button as follow. where buttonAndroidLogin_button is the Button object.

protected void onCreate(Bundle savedInstanceState) {
    // your code...
    buttonAndroidLogin_button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            hideKeyboard((Button)v);
            // ....
} // end onCreate

On the activity, you must set the next method

public void hideKeyboard(View view) {
    try {
        InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    } catch(Exception ignored) {
    }
}

It hides the input using the same button, so we don't need any linear layout, text view or any other arbitrary control. Also, the code is reusable for more buttons.

Sharkey answered 7/2, 2019 at 22:36 Comment(0)
D
3

You use this code

// Check if no view has focus:
View view = this.getCurrentFocus();
if (view != null) {  
    InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
}
Declivity answered 26/10, 2016 at 5:44 Comment(0)
B
3

IN KOTLIN :

In your fragment :

Create extension like this :

fun Fragment.hideKeyboard() {
    val imm = context?.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(requireView().windowToken, 0)
}

Then use it like this :

hideKeyboard()

In your activity :

Create extension like this :

fun AppCompatActivity.hideKeyboard() {
    val imm = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
    imm.hideSoftInputFromWindow(this.window.attributes.token, 0)
}

Then use it like this :

   hideKeyboard()
Barbarossa answered 23/9, 2020 at 6:26 Comment(2)
did not work for me, whereas using the layout to fetch the windowToken didTerrilynterrine
What did u mean about windowToken?!Barbarossa
B
0
InputMethodManager inputManager = (InputMethodManager) getActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.hideSoftInputFromWindow(rootView.getWindowToken(), 0);
Borderland answered 11/4, 2017 at 17:8 Comment(0)
W
0

This code is to remove keyboard from screen on Fragment.

The Code:

try {
  InputMethodManager inputMethodManager = (InputMethodManager) requireActivity().getSystemService(Context.INPUT_METHOD_SERVICE);
  inputMethodManager.hideSoftInputFromWindow(Objects.requireNonNull(requireActivity().getCurrentFocus()).getWindowToken(), 0);
} catch (Exception e) {
  Log.d("PROFILE KEYBOARD ERROR", e.toString());
}
Wherry answered 4/10, 2023 at 10:45 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Anthrax

© 2022 - 2024 — McMap. All rights reserved.