Search code examples
androidandroid-layoutandroid-dialogfragment

Why is my Dialog Fragment showing so thin?


I have a simple DialogFragment which contains 3 EditTexts and a Button. I have set the layout_width of its main layout 350dp but when I run the project it will be shown so slim.

Screen Shot

This My DialogFragment XML:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="350dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:animateLayoutChanges="true"
    android:background="#AFE6FF"
    android:orientation="vertical"
    android:padding="24dp">

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


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_mobile"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="8dp"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/et_mobile"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_title"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent">

            <EditText
                android:id="@+id/et_title"
                android:layout_width="match_parent"
                android:layout_height="50dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <android.support.constraint.ConstraintLayout
            android:id="@+id/layout_body"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layout_title">

            <EditText
                android:id="@+id/et_body"
                android:layout_width="match_parent"
                android:layout_height="150dp"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent" />

        </android.support.constraint.ConstraintLayout>


        <Button
            android:id="@+id/btn_send"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="Send"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/layout_body" />


    </LinearLayout>

</ScrollView>

and it will be shown by this code:

MyDialogFragment.newInstance().show(getSupportFragmentManager(), "my_dialog_fragment");

I need it to be shown Wider.


Solution

  • You need to change the LayoutParams of your dialog like the below code. put it in onResume():

    Window window = yourDialog.getWindow();
    if (window != null) {
        WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
        lp.copyFrom(window.getAttributes());
        //This makes the dialog take up the full width
        lp.width = WindowManager.LayoutParams.MATCH_PARENT;
        lp.height = WindowManager.LayoutParams.MATCH_PARENT;
        window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        window.setAttributes(lp);
    }
    

    I think that the default dialog's LayoutParams ( wrap_content,wrap_content ) overriding your definition from XML.

    It's better to use it this way and not use a fixed size values (350dp) to keep your layout responsive to all screen sizes.