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

3 Answers 3

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

Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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

2 Comments

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 )
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.