title: How to show soft keyboard perfectly in fragment in Android?
body: <p>I have an EditText inside a Fragment inside a Activity.</p>
  
  <p>My Activity layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                  android:layout_width="match_parent"
                  android:layout_height="match_parent"
                  android:background="@drawable/login_bg"&gt;
      ...
  
      &lt;FrameLayout
          android:id="@+id/fragment_container"
          android:layout_width="match_parent"
          android:layout_height="match_parent"/&gt;
  
      ...
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>My Activity config in AndroidManifest.xml:</p>
  
  <pre><code>    &lt;activity
          android:name="com.demo.LoginActivity"
          android:configChanges="orientation|keyboardHidden"
          android:launchMode="singleTop"
          android:screenOrientation="portrait"
          android:theme="@style/activityTheme" /&gt;
  </code></pre>
  
  <p>My code that use to start fragment in Activity:</p>
  
  <pre><code>private void startFragment(BaseFragment fragment, String tag) {
      FragmentManager fragmentManager = getSupportFragmentManager();
      FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
      fragmentTransaction.add(R.id.fragment_container, fragment);
      fragmentTransaction.addToBackStack(tag);
      fragmentTransaction.commitAllowingStateLoss();
  }
  </code></pre>
  
  <p>My Fragment layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:background="@color/common_background_color_white"
      android:orientation="vertical"
      android:clickable="true"
      android:paddingLeft="@dimen/email_common_padding_horizontal"
      android:paddingRight="@dimen/email_common_padding_horizontal"&gt;
  
      ...
  
      &lt;com.example.widget.LineEditView
          android:id="@+id/login_email_input"
          style="@style/BaseEditText.LoginEditText"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:focusable="true"
      /&gt;
  
      ...
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My Custom Widget <code>LineEditView</code> is a child class extend <code>RelativeLayout</code>,and it layout:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/input"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"&gt;
  
      &lt;EditText
          android:id="@+id/edit"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:focusable="true"
          android:gravity="start|center_vertical"
          android:background="@android:color/transparent"
          android:textColor="@color/common_text_color_black"
          android:maxLines="1"
          android:textCursorDrawable="@drawable/common_cursor_background_orange"
          android:textSize="@dimen/email_fields_text_size"
          android:paddingBottom="@dimen/email_fields_text_padding_bottom"/&gt;
  
      &lt;View
          android:id="@+id/underline"
          android:layout_below="@id/edit"
          android:layout_width="match_parent"
          android:layout_height="2px"/&gt;
  &lt;/RelativeLayout&gt; 
  </code></pre>
  
  <p>I want to show soft keyboard according to inputType property of EditText,and can  hide easily.</p>
  
  <p>What I have tried but not work or not perfect:</p>
  
  <p>1.According to <a href="https://stackoverflow.com/questions/10508363/show-keyboard-for-edittext-when-fragment-starts,i">Show keyboard for edittext when fragment starts</a> can show soft keyboard but can not hide easily(even can not hide sometimes) and it show keyboard not according to inputType property of EditText.</p>
  
  <p>2.I add following code to My Fragment:</p>
  
  <pre><code>@Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container) {
      mEditText = (EditText) rootView.findViewById(R.id.edit);
      mEditText.requestFocus();
      mEditText.setFocusable(true);
  }
  
  @Override
  public void onResume() {
      mEditText.postDelayed(mShowSoftInputRunnable, 400);
      super.onResume();
  }
  
  private Runnable mShowSoftInputRunnable = new Runnable() {
      @Override
      public void run() {
          FragmentActivity activity = getActivity();
          if (activity == null)
              return;
  
          InputMethodManager input = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
          input.showSoftInput(mEditText, InputMethodManager.SHOW_IMPLICIT);
      }
  };
  </code></pre>
  
  <p>but it can not show soft keyboard at all in my fragment.</p>
  
  <p>I can not put EditText to Activity because it need to refactor a lot of code.</p>
  
  <p>Does anyone have ideas to solve this problems?</p>