Prevent DialogFragment from Resizing/Collapsing when Keyboard is Shown
Asked Answered
C

2

6

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.

Cataphoresis answered 6/11, 2020 at 14:54 Comment(9)
Did you try android:windowSoftInputMode="adjustNothing" in the Manifest?Claqueur
@AliEid Yes and it has partial effects. It causes my FAB buttons to not adjust anymore, but dialog fragments still do. Worth noting that my app has the single activity architecture.Cataphoresis
I can confirm that this issue is not affecting API levels below 30. Tested on physical devices with API level 23, 26, 27, 28 and 30, as well as emulated divices with API levels 29 and 30. The common solution (setting windowSoftInputMode in any way - manifest, theme, layout, programmatically) seems to have no effect on the dialogs on API 30 and up.Longheaded
@TomLadek Thanks for the info! Glad someone else is also able to replicate this issue.Cataphoresis
Yesterday I've solved my resizing problem. Btw, I wasn't actually using the DialogFragment class but the factory of MaterialAlertDialogBuilder to create an alert dialog with my ordinary fragment as a custom view. Changing my fragment to DialogFragment and letting the system handle everything dialog-related automatically, the issue got fixed and my "custom" dialogs are now getting panned instead of resized. (Ironically, now I can't get them to resize instead of pan, but luckily I don't need it). Maybe if you post some code how you create the dialogs, we could figure out your problem.Longheaded
@TomLadek Sadly not using MaterialAlertDialogBuilder didn't fix my issue, the view is still being resized. All of my dialogs extend DialogFragments as well. If you could post some of your code, that would be extremely helpful!Cataphoresis
Hey there, I just wanted to add that I am also having this issue and I have tried everything I can find on stack overflow. Nothing has fixed it. Im using the MaterialAlertDialogBuilder as well. Did you happen to figure out the issue? cc @AdamSousaCollins
@Collins Sadly no, I've not been able to figure out a solution yet.Cataphoresis
I have the exact same issue as well. I spent a lot of time investigating but I haven't been able to solve it either. I can also confirm that it only happens on API 30. And it doesn't matter if DialogFragment or any of the dialog builders are used or not. The problem appears as long as the Dialog class itself is involved or even more specifically, if the windowIsFloating attribute is set to true via the dialog theme. My best guess is that this is a change in behavior (bug?) that was introduced with the new WindowInsets APIs in Android 11.Scutellation
L
0

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>
Longheaded answered 8/12, 2020 at 16:10 Comment(2)
Thank you for your code! Sadly, your solution still "resizes" or "adjusts" the dialog fragment. Notice how the dialog moves upward when the keyboard opens. All SDK's below 30 don't cause the dialog to move at all when the keyboard is shown. The main issue is that my dialog has a scroll view within it, so the dialog collapses when this resize/adjusts happens.Cataphoresis
I also tested this code and it was still resizing the dialog for me on API 30.Scutellation
P
0

I have the same problem in my app. But I found that my layout for dialog is ConstraintLayout and setting below.

app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"

I tried a lot of solutions from the Net. Finally, I think the layout change height because of keyboard displaying and parent layout zoom out. So I decide to set height for the dialog programmatically. I triggered setLayout function during dialog onStart. I think you could set height to device screen height.

Pantelegraph answered 1/10, 2022 at 8:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.