Mark F Mark F - 19 days ago 5
Android Question

Closing Soft Keyboard in Fragment

I found a number of answers to this question but none of them are working for me. I have an Edit text in my Fragment, which gets launched when the application starts. When this Fragment opens, the soft keyboard pops up as well. How do I prevent that from happening? This is what I have in my onCreateView method in my Fragment....

try {
InputMethodManager imm = (InputMethodManager)getActivity().getSystemService(
Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(userName.getWindowToken(), 0);
}catch(Exception e) {
e.printStackTrace();
}

Answer

Try this in onCreateView or onActivityCreated.

getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN);
Comments