title: Android - prevent dialog dimiss closing soft keyboard
body: <p>I am attempting to force the soft keyboard to display when an EditText gains focus, despite whether or not the user clicked on the EditText view.   What's strange is, if the action that caused EditText to gain focus originated from a Dialog onClick action, the keyboard will never be shown.   </p>
  
  <p>Here is a simple example:
  main.xml source:</p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="vertical"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:focusable="true"
                android:focusableInTouchMode="true"
                android:clickable="true"
          &gt;
      &lt;TextView
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Hello World, MyActivity"
              /&gt;
      &lt;Button android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Show Dialog"
              android:onClick="showDialog"/&gt;
      &lt;Button android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:text="Set Input Focus"
              android:onClick="setInputFocus"/&gt;
      &lt;EditText android:id="@+id/myEditText"
              android:layout_width="fill_parent"
                android:layout_height="wrap_content"/&gt;
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>And the activity:</p>
  
  <pre><code>public class MyActivity extends Activity {
  
      EditText myEditText;
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.main);
          myEditText = (EditText)findViewById(R.id.myEditText);
          myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
              @Override
              public void onFocusChange(View v, boolean hasFocus) {
                  if(hasFocus){
                      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                      imm.showSoftInput(myEditText,InputMethodManager.SHOW_IMPLICIT);
                  } else {
                      InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
                      imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0);
                  }
              }
          });
      }
  
      public void showDialog(View v){
          new AlertDialog.Builder(this).setItems(new CharSequence[]{"Set Input Focus"}, new DialogInterface.OnClickListener() {
              @Override
              public void onClick(DialogInterface dialog, int which) {
                  myEditText.requestFocus();
              }
          }).create().show();
      }
  
      public void setInputFocus(View v){
          myEditText.requestFocus();
      }
  
  }
  </code></pre>
  
  <p>When clicking on the "Set Input Focus" button, the view edit text will gain focus, and the keyboard will show up.   However if you were to click the "Set Input Focus" item in the dialog, the keyboard will show up briefly and then disappear immediately (often times it will not show it at all).  It appears as if the dialog dismiss is closing the keyboard that we told to open during the click event, but the EditText still remains in focus. </p>
  
  <p>So my question is, how do I prevent the dialog from closing the soft input window? :-)  Any suggestions are greatly appreciated.   Thank you!</p>