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

1.6k Views Asked by At

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!

1

There are 1 best solutions below

0
ʍѳђઽ૯ท On BEST ANSWER

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