Why is my Dialog Fragment showing so thin?
Asked Answered
C

2

6

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.

Clara answered 7/7, 2019 at 8:22 Comment(2)
How to tried changing the width you set to the scrollview? talking about this android:layout_width="350dp" , try to change it to a bigger number and till me if it change anything. please post the code of the activity that "calls" the dialogueArbuckle
@Arbuckle It doesn't change the result.Clara
L
3

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.

Lorient answered 7/7, 2019 at 8:29 Comment(7)
I put your code to onResume() and it worked but now it is fullscreen like an activity because it's width and height is set MATCH_PARENT. I can set a fixed width for LinearLayout and set lp.width and lp.height to WRAP_CONTENT but I want to know is there a better standard way or not?Clara
Maybe you can do something like this - lp.height = (int) (WindowManager.LayoutParams.MATCH_PARENT *0.5); And just change 0.5 to better controll your paramsLorient
You know... I'm so curious to know why this happened? this is not the first DialogFragment that I created in this project!!!Clara
I don't really have one answer to fit all cases, sometimes when working with dialogs all is working and another time I am getting the same problem as you do and I need to use layout prams as mentioned in my answer to fix it.Lorient
oh, I was wrong! I used to put getDialog().getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); in my other fragment dialogs, too!Clara
glad it helped you with your problemLorient
lp.width = WindowManager.LayoutParams.MATCH_PARENT; lp.height = WindowManager.LayoutParams.MATCH_PARENT; will prevent setCancelable to work, instead place WRAP_CONTENT in the heightGyratory
S
0

You want to override onCreateDialog and add your custom layout there instead. Like below. I use ViewBinding to create my views.

override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        return AlertDialog.Builder(requireContext())
            .setView(binding.root)
            .setCancelable(false)
            .create().apply {
                setCanceledOnTouchOutside(false)
            }
    }
Someplace answered 24/3, 2021 at 18:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.