title: Hide Soft Keyboard on Done Keypress in Android?
body: <p>I'm struggling with the done button on the soft keyboard. I can't get the soft keyboard Done key press to hide the keyboard. From another button, it works perfectly with </p>
  
  <p><code>imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);</code> </p>
  
  <p>but the onKeyListener does not function the way I want. When I hit the editText, the soft keyboard shows up and its content is cleared from characters.</p>
  
  <p>Thanks for listening!</p>
  
  <p>The main.xml:</p>
  
  <pre><code>&lt;EditText 
      android:id="@+id/answer" 
      android:layout_gravity="center_horizontal" android:textSize="36px"
      android:inputType="phone"
      android:minWidth="60dp" android:maxWidth="60dp"
  /&gt;
  </code></pre>
  
  <p>The Java file:</p>
  
  <pre><code>private EditText editText;
  //...
  editText = (EditText)findViewById(R.id.answer);
  editText.setOnClickListener(onKeyboard);
  editText.setOnKeyListener(onSoftKeyboardDonePress);
  //...
  
  // method not working:
  private View.OnKeyListener onSoftKeyboardDonePress=new View.OnKeyListener() 
  {
      public boolean onKey(View v, int keyCode, KeyEvent event) 
      {
          if (event.getKeyCode() == KeyEvent.FLAG_EDITOR_ACTION)
          {
              // code to hide the soft keyboard
              imm = (InputMethodManager) getSystemService(
                  Context.INPUT_METHOD_SERVICE);
              imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
          }
          return false;
      }
  };
  
  private View.OnClickListener onKeyboard=new View.OnClickListener() 
  {
      public void onClick(View v) 
      {
          editText.setText("");
      }
  };
  </code></pre>
  
  <p>The working method using a button (in the same java file):</p>
  
  <pre><code>private View.OnClickListener onDone=new View.OnClickListener() 
  {
      public void onClick(View v) 
      {
          //....
          // code to hide the soft keyboard
          imm = (InputMethodManager) getSystemService(
              Context.INPUT_METHOD_SERVICE);
          imm.hideSoftInputFromWindow(editText.getApplicationWindowToken(), 0);
      }
  };
  </code></pre>
  
  <p><strong>Edit:</strong> When I press key no "9" the keyboard hides. That's odd.</p>