4

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);
Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393

3 Answers3

5

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

Suragch
  • 484,302
  • 314
  • 1,365
  • 1,393
2

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
      }
   }
Budius
  • 39,391
  • 16
  • 102
  • 144
0

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

Community
  • 1
  • 1
Silvia H
  • 8,097
  • 7
  • 30
  • 33
  • i get cannot get cast to Android.app.Activity error , my context class extends InputMethodService :( i cant use activity (if there is a way i dont know ) – Ömer Faruk Çakmakçı Jun 29 '15 at 15:55
  • If you're in a keyboard, you hide yourself via requestHideSelf. But your keyboard probably doesn't have a gridView, so i think you're a bit confused. – Gabe Sechan Jul 01 '15 at 00:15