How to close/hide custom keyboard Android

I've try to close my custom keyboard after click item in gridview.I'm trying to do it in BaseAdapter class. context is come from InputMethodService.

So far I've tried below:

FrameLayout scroll = (FrameLayout)inflater.inflate(R.layout.keyboard, null);
 InputMethodManager imm = (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(scroll.getWindowToken(), 0);

--

imm.toggleSoftInput(0,InputMethodManager.HIDE_IMPLICIT_ONLY);

--

 scroll.setVisibility(View.INVISIBLE);

Upvotes: 4

Views: 1854

Answers (3)

Suragch
Suragch

Reputation: 511626

If you have your own custom keyboard and you have extended InputMethodService, then you can just call

requestHideSelf(0)

from within your service to force close the keyboard or

requestHideSelf(InputMethodManager.HIDE_IMPLICIT_ONLY);

to close the keyboard only if the user didn't explicitly request it to be shown.

Documentation

Upvotes: 5

Silvia H
Silvia H

Reputation: 8357

You can put this method in a public class, and call it wherever needed.

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);
}

refer to my answer here

Upvotes: 0

Budius
Budius

Reputation: 39836

I'm just copying and pasting from my app here, it works fine for us:

   public static void hideKeyboard(View v) {
      try {
         v.clearFocus();
         InputMethodManager imm = (InputMethodManager) v.getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
         imm.hideSoftInputFromWindow(v.getWindowToken(), 0);
      } catch (Exception e) {
         // we all saw shit happening on this code before
      }
   }

Upvotes: 2

Related Questions