Prevent DialogFragment from Resizing/Collapsing when Keyboard is Shown

Viewed 603

I've been having issues getting my dialog fragments to NOT resize/collapse when a keyboard is shown. I've been having this problem with specific APIs like Android 30 but not with older APIs like Android 27. I've provided screenshots below and I can confirm that his affects ALL dialog fragments within my app of a specific API.

Android API 30 Dialog Screenshot (The resize issue)

Android API 27 Dialog Screenshot (The desired functionality)

EDIT: Tom Ladek pointed out that this issue is ONLY happening on SDK 30. SDK's 29 and below are unaffected.

1 Answers

The key is probably in the creation of the Dialog. When you extend DialogFragment, you just have to create a new Object of that type, call show on it and that's it. In onCreateView of that fragment you inflate your ordinary fragment layout file.

So an app that creates dialogs with custom fragments that don't resize when the soft input is opened, even on API level 30 (and up, probably), could look like this:

Screen cast taken on API level 30

MainActivity:

public class MainActivity extends androidx.appcompat.app.AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        findViewById(R.id.show_dialog).setOnClickListener(v -> {
            new LoginFragment().show(getSupportFragmentManager(), "login");
        });
    }
}

LoginFragment:

public class LoginFragment extends androidx.fragment.app.DialogFragment.DialogFragment {

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_dialogcontent, container, false);
    }
}

activity_main:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <Button
        android:id="@+id/show_dialog"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show dialog"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

fragment_dialogcontent:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:minWidth="300dp"
    android:padding="16dp">

    <TextView
        android:id="@+id/login_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Login"
        android:textAppearance="@style/TextAppearance.AppCompat.Large"
        app:layout_constraintBottom_toTopOf="@+id/login_description"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_chainStyle="packed" />

    <TextView
        android:id="@+id/login_description"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:text="Please login"
        app:layout_constraintBottom_toTopOf="@+id/login_username"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_title" />

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_username"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:hint="Username"
        app:errorEnabled="true"
        app:layout_constraintBottom_toTopOf="@+id/login_password"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_description">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_edittext_username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="username"
            android:inputType="text|number" />
    </com.google.android.material.textfield.TextInputLayout>

    <com.google.android.material.textfield.TextInputLayout
        android:id="@+id/login_password"
        style="@style/Widget.MaterialComponents.TextInputLayout.FilledBox.Dense"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:hint="Password"
        app:endIconMode="password_toggle"
        app:errorEnabled="true"
        app:layout_constraintBottom_toTopOf="@+id/login_button_ok"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_username">

        <com.google.android.material.textfield.TextInputEditText
            android:id="@+id/login_edittext_password"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:autofillHints="password"
            android:inputType="textPassword" />
    </com.google.android.material.textfield.TextInputLayout>

    <Button
        android:id="@+id/login_button_ok"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Login"
        app:layout_constraintBottom_toTopOf="@+id/login_button_cancel"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_password" />

    <Button
        android:id="@+id/login_button_cancel"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:text="Cancel"
        app:layout_constraintBottom_toTopOf="@+id/login_register_link"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_button_ok" />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/login_register_link"
        style="@style/Widget.MaterialComponents.Button.TextButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="24dp"
        android:text="Register"
        android:textAppearance="@style/TextAppearance.AppCompat.Small"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/login_button_cancel" />

</androidx.constraintlayout.widget.ConstraintLayout>
Related