Mak
Mak

Reputation: 1063

How to close Android Soft KeyBoard programmatically?

I am currently showing softkeyboard using the following code

InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

And Here I d'not bind the softkeyboard with Edittext because of that I had used the above code.

Now I want to close the SoftKeyboard so i am currently using the below code but it is not working.

imm.toggleSoftInput (InputMethodManager.SHOW_FORCED, InputMethodManager.RESULT_HIDDEN);

Can Anyone suggest me what to use for closing the softKeyboard ?


Based on Below Answer I want to let you clear that I am not using EditText, I use Layout on which I want to show Keyboard and Hide keyboard. I want to send keyboard key event to remote area bcoz of that I didnot used editText.

Upvotes: 53

Views: 103088

Answers (17)

Vinay
Vinay

Reputation: 767

Kotlin way Hide and Show the Android Soft Keyboard:

Create a Extension file in kotlin and add below code for hide/show the Keyboard.

> fun EditText.showKeyboard(){
  requestFocus()
  val insetsController = ViewCompat.getWindowInsetsController(this)
  insetsController?.show(WindowInsetsCompat.Type.ime())
}

fun Activity.hideKeyboard(){
  val insetsController = ViewCompat.getWindowInsetsController(window.decorView)
  insetsController?.hide(WindowInsetsCompat.Type.ime())
}

Upvotes: 0

rNkL
rNkL

Reputation: 472

To hide the keyboard programmatically::

fun hideKeypad() {
    val view = currentFocus
    if (view != null) {
        val imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }
}

Upvotes: 0

Adil Mushtaq
Adil Mushtaq

Reputation: 41

  public void hideKeyboard(Activity activity) {
    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
    //Find the currently focused view, so we can grab the correct window token from it.
    View view = activity.getCurrentFocus();
    //If no view currently has focus, create a new one, just so we can grab a window token from it
    if (view == null) {
        view = new View(activity);
    }
    imm.hideSoftInputFromWindow(view.getWindowToken(), 0);
    imm.toggleSoftInput(InputMethodManager.RESULT_HIDDEN, 0);
    getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
}

and Call where you want

 hideKeyboard(MainActivity.this);

Upvotes: 0

Gibolt
Gibolt

Reputation: 47097

Use a Kotlin Extension to hide keyboard

// Simple, reuseable call anywhere in code
myView.hideKeyboard()

fun View.hideKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.hideSoftInputFromWindow(windowToken, 0)
}

How to show soft keyboard

fun View.showKeyboard() {
    val inputMethodManager = context.getSystemService(INPUT_METHOD_SERVICE) as InputMethodManager
    inputMethodManager.toggleSoftInput(SHOW_FORCED, HIDE_IMPLICIT_ONLY)
}

Related further topics:

Simplify adding Done/Next... action to edittext: read this post

Remove requirement for ever using getSystemService: Splitties Library

Upvotes: 0

Ashutosh Srivastava
Ashutosh Srivastava

Reputation: 617

Close/hide the Android Soft Keyboard

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

it's working for me i hope it's work for you..

Open the Android Soft Keyboard

 InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
        if (inputMethodManager != null) {
            inputMethodManager.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
        }

Upvotes: 5

tony gil
tony gil

Reputation: 9554

This code hides the keyboard from inside onItemClick of an AutoCompleteTextView

public void onItemClick(AdapterView<?> adapterViewIn, View viewIn, int indexSelected, long arg3) {
     // whatever your code does
     InputMethodManager imm = (InputMethodManager) getSystemService(viewIn.getContext().INPUT_METHOD_SERVICE);
     imm.hideSoftInputFromWindow(viewIn.getApplicationWindowToken(), 0);
}

Upvotes: 0

varotariya vajsi
varotariya vajsi

Reputation: 4041

For hiding keyboard,

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

Here, “mView” can be any view which is visible on screen

Upvotes: 0

Priyanka
Priyanka

Reputation: 391

InputMethodManager im =(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
im.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

Upvotes: 0

Boris Treukhov
Boris Treukhov

Reputation: 17774

Here is s solution, which checks if keyboard is visible

    public static void hideKeyboard(Activity activity) {
        if (isKeyboardVisible(activity)) {
            InputMethodManager imm = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            imm.toggleSoftInput(InputMethodManager.HIDE_IMPLICIT_ONLY, 0);
        }
    }

    public static boolean isKeyboardVisible(Activity activity) {
        ///This method is based on the one described at http://stackoverflow.com/questions/4745988/how-do-i-detect-if-software-keyboard-is-visible-on-android-device
        Rect r = new Rect();
        View contentView = activity.findViewById(android.R.id.content);
        contentView.getWindowVisibleDisplayFrame(r);
        int screenHeight = contentView.getRootView().getHeight();

        int keypadHeight = screenHeight - r.bottom;

        return
                (keypadHeight > screenHeight * 0.15);
    }

Upvotes: 0

Daniel Cherubini
Daniel Cherubini

Reputation: 21

This works fine

InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.hideSoftInputFromWindow(getWindow().getAttributes().token, 0);

Upvotes: 2

Vincent Williams
Vincent Williams

Reputation: 3136

private void close() {
    this.requestHideSelf(0);
}

this method is very simple

Upvotes: -4

uowaep
uowaep

Reputation: 41

user942821's answer for hiding it works:

imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

But this also works for me to hide it:

imm.toggleSoftInput(0, 0);

You might also want to try:

imm.toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);

When using '0' in the first parameter sometimes the keyboard toggles on in the wrong places under weird circumstances that I haven't been able to figure out how to duplicate yet. I'm still testing this last example, but will update when I find out more.

See toggleSoftInput documentation page for more information.

Upvotes: 2

Raj
Raj

Reputation: 698

Use this working code :

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

Upvotes: 34

Raj
Raj

Reputation: 698

If you want, you can use entire class and call KeyboardUtil.hideKeyBoard(context) method wherever:

public class KeyboardUtil
{
public static void hideKeyboard(Activity activity)
    {
        try
        {
            InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
            inputManager.hideSoftInputFromWindow(activity.getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
         }
        catch (Exception e)
        {
            // Ignore exceptions if any
                Log.e("KeyBoardUtil", e.toString(), e);
        }
    }
}

Upvotes: 9

user942821
user942821

Reputation:

I have tested and this is working:

...
//to show soft keyboard
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

//to hide it, call the method again
imm.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);

By the way, the second parameter of your code is not right, please have a look at here.

Upvotes: 97

mseo
mseo

Reputation: 3911

You could also try

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

Upvotes: 0

Jana
Jana

Reputation: 2920

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

Upvotes: 41

Related Questions