title: Best way to hide keyboard in Android
body: <p>I would like to know the best way to hide keyboard after entering the text to EditText. </p>
  
  <p>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. </p>
  
  <p>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:</p>
  
  <pre><code>@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;
          }
      });
  }
  </code></pre>
  
  <p>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. </p>
  
  <p>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?</p>