1

I have the following custom view DialogFragment.

enter image description here

When I tap on the EditText, soft keyboard is shown. Currently, the observations are

  1. The position of the dialog is "push up" a bit.
  2. Dialog is not resized and Dialog content is blocked.

This is how it looks like.

enter image description here

I don't want the dialog content covered by keyboard. I had made modification according to https://stackoverflow.com/a/36295587/72437

getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

Still, it doesn't help at all.

I had also tried

dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);

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 EditText immediately.

May I know, how can I make my Dialog resized, when soft keyboard is shown?


Update

I did an experiment by applying the same XML layout file on an Activity. The Activity is resized without problem.

Seem like the XML layout file itself is OK.

The soft input mode I am using is

<activity android:name="org.yccheok.jstock.gui.trading.sign_in.SignInFragmentActivity"
    android:screenOrientation="portrait"
    android:windowSoftInputMode="adjustResize|stateAlwaysVisible" />

enter image description here


The is the complete code and layout file.

Source 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 >= 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 > width) {
            dialog.getWindow().setLayout(width, height);
        }
    }
}

Layout XML

<?xml version="1.0" encoding="utf-8"?>
<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">

    <RelativeLayout
        android:id="@+id/sign_in_relative_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <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">

            <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" >
                <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" />
            </android.support.design.widget.TextInputLayout>

            <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" >
                <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" />
            </android.support.design.widget.TextInputLayout>

        </LinearLayout>

        <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" />

        <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">
            <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" />
        </LinearLayout>
    </RelativeLayout>
</ViewAnimator>
Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875

1 Answers1

1

After some debugging, I realize it is caused by

  1. Call to makeDialogShorter, which set the height of Dialog, is unnecessary.
  2. Adjust soft input during onGlobalLayout is unnecessary.

Here's my solution. I refactor the code to use onCreateView instead of onCreateDialog, to make the code shorter and simpler.

package org.yccheok.jstock.gui.trading.sign_in;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        final View view = inflater.inflate(R.layout.trading_sign_in_fragment, container);

        Button forgotPasswordButton = (Button)view.findViewById(R.id.forgot_password_button);
        Button signInButton = (Button)view.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);


        //set to adjust screen height automatically, when soft keyboard appears on screen
        getDialog().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);

        return view;
    }
}

Here's the outcome

enter image description here

We now have a short cute little dialog :)

Cheok Yan Cheng
  • 47,586
  • 132
  • 466
  • 875