Best way to hide keyboard in Android
android, keyboard
Solution
You can use this code
InputMethodManager imm =
(InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(mEditView.getWindowToken(), 0);
Problem
I would like to know the best way to hide keyboard after entering the text to EditText. 1) setonfocuschangelistener : Does this listener is fired only, when the done button is pressed or when the focus changes from one EditText to other? When I used this method, I couldn't hide the keyboard. 2) setOnTouchListener : When I used this, I could hide the keyboard, but i doubt there might be an issue with this. In this case, I add the touch listener to the root LinearLayout. Following code I had used: ``` @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.login); txtUserName = (EditText)findViewById(R.id.txtUserName); btnLogin = (Button)findViewById(R.id.btnLogin); layoutView = (LinearLayout)findViewById(R.id.li); layoutView.setOnTouchListener(new OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { InputMethodManager inputManager = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); inputManager.hideSoftInputFromWindow(txtUserName .getWindowToken(), 0); return true; } }); } ``` Inside the main LinearLayout, I am using other two LinearLayouts. The issue that i faced with the above code is that at some points when I pressed, the keyboard doesn't hides. My doubt is that I am adding touch listener only with root layout, not giving touch listener with other inner layouts or other controls(TextView). When I touch over other controls or some points around the TextView(ie, inner layouts), keyboard doesn't hides. That means do i need to add touchListener to all layouts or controls inside the root layout? How this situation can be handled in a better way?