How to make DialogFragment popup higher than screen in Android Java?

I am implementing a DialogFragment where I have lot of contents inside where I would scroll that DialogFragment so I am using:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    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="wrap_content"
    android:background="@color/area_frg"
    android:orientation="vertical"
    tools:context=".MtPopupDialogFragmentPage">

    <com.google.android.material.appbar.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/Theme.AREATracerManager.AppBarOverlay">

        <com.google.android.material.appbar.MaterialToolbar
            android:id="@+id/topDialogAppBar"
            style="@style/Widget.MaterialComponents.Toolbar.Primary"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:navigationIcon="?attr/homeAsUpIndicator"
            app:subtitle="Back to main"
            app:subtitleTextColor="@color/white" />

    </com.google.android.material.appbar.AppBarLayout>

    .
    .
    .
    .
    .

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                android:id="@+id/btnResetFW2"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_margin="8dp"
                android:layout_gravity="center|center_horizontal"
                android:contentDescription="@string/app_name"
                android:text="@string/st_restart"
                android:textColor="@color/area_bck"
                android:textSize="17dp"
                android:textAllCaps="true"
                android:padding="8dp"
                app:layout_anchorGravity="center|center_horizontal"/>

            <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
                android:id="@+id/btnCheckSW2"
                android:layout_width="wrap_content"
                android:layout_height="50dp"
                android:layout_margin="8dp"
                android:layout_gravity="center|center_horizontal"
                android:contentDescription="@string/app_name"
                android:text="@string/st_check"
                android:textColor="@color/bck"
                android:textSize="17dp"
                android:textAllCaps="true"
                android:padding="8dp"
                app:layout_anchorGravity="center|center_horizontal"/>

        </LinearLayout>

</LinearLayout>

but lasts contents are shrinked to screen’s height but I’d want to scroll and view all the content… I tried also to change top node with ConstraintLayout instead of LinearLayout but it didn’t fix my goal…maybe that’s not possible with DialogFragment?… Thanks to all! Cheers! 🙂

>Solution :

Sure thing! To ensure your DialogFragment’s content is properly scrollable and displayed, you shoukd follow these steps:

  1. ScrollView Setup: Wrap your entire content layout within a ScrollView. This enables users to scroll through the content if it exceeds the screen height.
<ScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <!-- Place your content layout components here -->
</ScrollView>
  1. Dialog Height Adjustment: To prevent any content from being cropped, adjust the DialogFragment’s height within the onCreateDialog method using WindowManager.LayoutParams.
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    Dialog dialog = super.onCreateDialog(savedInstanceState);

    // Set dialog height to a percentage of screen height
    WindowManager.LayoutParams params = dialog.getWindow().getAttributes();
    DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
    int dialogHeight = (int) (displayMetrics.heightPixels * 0.8); // Adjust as needed
    params.height = dialogHeight;
    dialog.getWindow().setAttributes(params);

    return dialog;
}
  1. ConstraintLayout Convenience: If you’re comfortable with ConstraintLayout, it’s suitable for structuring your content. Just ensure the last item within the ScrollView is anchored to the bottom.
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
    android:id="@+id/lastFab"
    app:layout_constraintBottom_toBottomOf="parent"
    <!-- Other attributes here -->
/>

If you follow these steps it should work correctly. Hope it helps

Leave a Reply