Search code examples
javaandroidinputmethodmanager

Using getCurrentFocus or getSystemService to get View doesn´t work?


I want to hide the keyboard:

View view = this.getCurrentFocus();

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

Now I have the problem, that getCurrentFocus and getSystemService are colored red and it says:

Cannot resolve method getCurrentFocus() / getSystemService()

What am I doing wrong?

Thanks for your help!


Solution

  • This getSystemService should have a context before:

    InputMethodManager imm = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE); // or context
    

    or getActivity() or similiar codes to get the context.

    Check out the codes: Close/hide the Android Soft Keyboard