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:
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>
windowSoftInputMode
in any way - manifest, theme, layout, programmatically) seems to have no effect on the dialogs on API 30 and up. – LongheadedDialogFragment
or any of the dialog builders are used or not. The problem appears as long as theDialog
class itself is involved or even more specifically, if thewindowIsFloating
attribute is set totrue
via the dialog theme. My best guess is that this is a change in behavior (bug?) that was introduced with the newWindowInsets
APIs in Android 11. – Scutellation