title: My custom view DialogFragment is not resize when keyboard is shown even though using SOFT_INPUT_ADJUST_RESIZE on Dialog Window
body: <p>I have the following custom view <code>DialogFragment</code>.</p>
  
  <p><a href="https://i.stack.imgur.com/0sNAH.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0sNAH.png" alt="enter image description here"></a></p>
  
  <p>When I tap on the <code>EditText</code>, soft keyboard is shown. Currently, the observations are</p>
  
  <ol>
  <li>The position of the dialog is "push up" a bit.</li>
  <li><strong>Dialog is not resized and Dialog content is blocked.</strong></li>
  </ol>
  
  <p>This is how it looks like.</p>
  
  <p><a href="https://i.stack.imgur.com/9cdSB.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9cdSB.png" alt="enter image description here"></a></p>
  
  <p>I don't want the dialog content covered by keyboard. I had made modification according to <a href="https://stackoverflow.com/a/36295587/72437">https://stackoverflow.com/a/36295587/72437</a></p>
  
  <pre><code>getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
  </code></pre>
  
  <p>Still, it doesn't help at all.</p>
  
  <p>I had also tried</p>
  
  <pre><code>dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
  </code></pre>
  
  <p>It makes no difference. The Dialog is not resized and the Dialog content is blocked. Except the soft keyboard will be shown immediately during the first time, without requiring user tapping on the <code>EditText</code> immediately.</p>
  
  <p><strong><em>May I know, how can I make my <code>Dialog</code> resized, when soft keyboard is shown?</em></strong></p>
  
  <hr>
  
  <h2>Update</h2>
  
  <p>I did an experiment by applying the same XML layout file on an Activity. The <code>Activity</code> is resized without problem.</p>
  
  <p><strong>Seem like the XML layout file itself is OK.</strong></p>
  
  <p>The soft input mode I am using is</p>
  
  <pre><code>&lt;activity android:name="org.yccheok.jstock.gui.trading.sign_in.SignInFragmentActivity"
      android:screenOrientation="portrait"
      android:windowSoftInputMode="adjustResize|stateAlwaysVisible" /&gt;
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/P0s5R.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/P0s5R.png" alt="enter image description here"></a></p>
  
  <hr>
  
  <p>The is the complete code and layout file.</p>
  
  <h2>Source code</h2>
  
  <pre><code>package org.yccheok.jstock.gui.trading.sign_in;
  
  import android.annotation.SuppressLint;
  import android.app.Activity;
  import android.app.Dialog;
  import android.os.Bundle;
  import android.support.design.widget.TextInputLayout;
  import android.support.v4.app.DialogFragment;
  import android.support.v4.app.Fragment;
  import android.support.v7.app.AlertDialog;
  import android.util.DisplayMetrics;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.ViewTreeObserver;
  import android.view.WindowManager;
  import android.widget.Button;
  
  import org.yccheok.jstock.gui.R;
  
  /**
   * Created by yccheok on 8/1/2018.
   */
  
  public class SignInDialogFragment extends DialogFragment {
  
      public static SignInDialogFragment newInstance() {
          SignInDialogFragment signInDialogFragment = new SignInDialogFragment();
          return signInDialogFragment;
      }
  
      @Override
      public Dialog onCreateDialog(Bundle savedInstanceState) {
          final Activity activity = getActivity();
  
          // Get the layout inflater
          LayoutInflater inflater = LayoutInflater.from(activity);
  
          final View view = createView(this, inflater, null);
  
          final AlertDialog.Builder builder = new AlertDialog.Builder(activity);
  
          final AlertDialog dialog = builder.setView(view).create();
  
          dialog.setCanceledOnTouchOutside(true);
  
          final ViewTreeObserver vto = view.getViewTreeObserver();
          vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  
              @SuppressLint("NewApi")
              @SuppressWarnings("deprecation")
              @Override
              public void onGlobalLayout() {
                  if (android.os.Build.VERSION.SDK_INT &gt;= android.os.Build.VERSION_CODES.JELLY_BEAN) {
                      view.getViewTreeObserver().removeOnGlobalLayoutListener(this);
                  } else {
                      view.getViewTreeObserver().removeGlobalOnLayoutListener(this);
                  }
  
                  makeDialogShorter(dialog);
                  dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
              }
          });
  
          dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
  
          return dialog;
      }
  
      private static View createView(final Fragment fragment, final LayoutInflater inflater, final ViewGroup container) {
          View v = inflater.inflate(R.layout.trading_sign_in_fragment, container, false);
  
          Button forgotPasswordButton = (Button)v.findViewById(R.id.forgot_password_button);
          Button signInButton = (Button)v.findViewById(R.id.sign_in_button);
  
          forgotPasswordButton.setOnClickListener(new View.OnClickListener() {
              @Override
              public void onClick(View view) {
              }
          });
  
          signInButton.setOnClickListener(new View.OnClickListener() {
  
              @Override
              public void onClick(View view) {
              }
          });
  
          signInButton.setEnabled(false);
  
          return v;
      }
  
      private static void makeDialogShorter(Dialog dialog) {
          // http://stackoverflow.com/questions/19326142/why-listview-expand-collapse-animation-appears-much-slower-in-dialogfragment-tha
          int width = dialog.getWindow().getDecorView().getWidth();
  
          DisplayMetrics displayMetrics = new DisplayMetrics();
          dialog.getOwnerActivity().getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
          int height = displayMetrics.heightPixels;
  
          height = Math.min(
                  (int)(height * 4.0 / 5.0),
                  dialog.getWindow().getDecorView().getHeight()
          );
  
          if (height &gt; width) {
              dialog.getWindow().setLayout(width, height);
          }
      }
  }
  </code></pre>
  
  <h2>Layout XML</h2>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:app="http://schemas.android.com/apk/res-auto"
      android:id="@+id/view_animator"
      android:animateFirstView="false"
      android:layout_width="fill_parent"
      android:layout_height="fill_parent"&gt;
  
      &lt;RelativeLayout
          android:id="@+id/sign_in_relative_layout"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent"&gt;
          &lt;LinearLayout
              android:orientation="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:paddingTop="@dimen/trading_activity_vertical_margin"
              android:paddingBottom="0dp"
              android:layout_above="@+id/sign_in_bottom_nav_bar"&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
                  android:layout_marginRight="@dimen/trading_activity_horizontal_margin"
  
                  app:hintTextAppearance="@style/TradingWizardTextInputLayout"
                  android:id="@+id/username_text_input_layout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
                  &lt;android.support.design.widget.TextInputEditText
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/wizard_username"
                      android:id="@+id/username_edit_text"
  
                      android:inputType="textVisiblePassword|textNoSuggestions"
                      android:imeOptions="actionNext|flagNoExtractUi" /&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
              &lt;android.support.design.widget.TextInputLayout
                  android:layout_marginLeft="@dimen/trading_activity_horizontal_margin"
                  android:layout_marginRight="@dimen/trading_activity_horizontal_margin"
  
                  app:hintTextAppearance="@style/TradingWizardTextInputLayout"
                  app:passwordToggleEnabled="true"
                  android:id="@+id/password_text_input_layout"
                  android:layout_width="match_parent"
                  android:layout_height="wrap_content" &gt;
                  &lt;android.support.design.widget.TextInputEditText
                      android:layout_width="match_parent"
                      android:layout_height="wrap_content"
                      android:hint="@string/wizard_password"
                      android:id="@+id/password_edit_text"
  
                      android:inputType="textPassword"
                      android:imeOptions="actionNext|flagNoExtractUi" /&gt;
              &lt;/android.support.design.widget.TextInputLayout&gt;
  
          &lt;/LinearLayout&gt;
  
          &lt;Button
              style="?android:attr/buttonBarButtonStyle"
              android:id="@+id/forgot_password_button"
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:gravity="center"
              android:layout_gravity="center"
              android:enabled="true"
              android:textAllCaps="false"
              android:text="@string/forgot_password"
              android:textSize="16sp"
              android:layout_above="@+id/sign_in_bottom_nav_bar"
              android:layout_centerHorizontal="true"
  
              android:layout_marginBottom="8dp"
              android:paddingLeft="32dp"
              android:paddingRight="32dp" /&gt;
  
          &lt;LinearLayout
              android:background="?attr/welcomeBottomNavBarBackground"
              android:orientation="horizontal"
              android:id="@+id/sign_in_bottom_nav_bar"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_alignParentBottom="true"&gt;
              &lt;Button
                  style="?android:attr/buttonBarButtonStyle"
                  android:background="?attr/selectableItemBackground"
  
                  android:id="@+id/sign_in_button"
                  android:layout_width="0dp"
                  android:width="0dp"
                  android:layout_weight="1.0"
                  android:layout_height="48dp"
                  android:gravity="center"
                  android:layout_gravity="center"
                  android:enabled="true"
                  android:textAllCaps="true"
                  android:text="@string/log_in" /&gt;
          &lt;/LinearLayout&gt;
      &lt;/RelativeLayout&gt;
  &lt;/ViewAnimator&gt;
  </code></pre>