Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

DialogFragment will not respect wrap_content

I've got a custom DialogFragment class that looks like this:

/**
 * Dialog Fragment containing rating form.
 */
public class RatingDialogFragment extends DialogFragment {

    public static final String TAG = "RatingDialog";

    // ...


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater,
                             @Nullable ViewGroup container,
                             @Nullable Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.dialog_rating, container, false);
        ButterKnife.bind(this, v);

        return v;
    }

    // ...
}

The root view is a LinearLayout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:padding="16dp">

Every single child view has android:layout_height="wrap_content".

Yet it looks like this. What could I be doing wrong?

enter image description here

like image 476
Sam Stern Avatar asked Aug 24 '17 00:08

Sam Stern


People also ask

Is DialogFragment deprecated?

This method is deprecated. androidx.

What is the difference between dialog & DialogFragment?

Dialog: A dialog is a small window that prompts the user to make a decision or enter additional information. DialogFragment: A DialogFragment is a special fragment subclass that is designed for creating and hosting dialogs.

How do I know if DialogFragment is showing?

showDialog() twice back-to-back and you will get two dialogs. Because show is asynchronous and your findFragmentByTag(TAG) == null check will be true until dialog is actually added by system.

What is a DialogFragment?

Android DialogFragments. DialogFragment is a utility class which extends the Fragment class. It is a part of the v4 support library and is used to display an overlay modal window within an activity that floats on top of the rest of the content. Essentially a DialogFragment displays a Dialog but inside a Fragment.


2 Answers

This was partially an issue in my layout. At the bottom of my dialog I had a button bar:

<!-- Cancel and apply buttons -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">

    <View
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1" />

    <Button
        android:id="@+id/button_cancel"
        style="@style/Base.Widget.AppCompat.Button.Borderless"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/cancel"
        android:textColor="@color/greySecondary"
        android:theme="@style/ThemeOverlay.FilterButton" />


    <Button
        android:id="@+id/button_search"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="@string/apply"
        android:theme="@style/ThemeOverlay.FilterButton" />

</LinearLayout>

The first View (a spacer) was expanding to fill the viewport, changing it to this fixed it:

    <View
        android:layout_width="0dp"
        android:layout_height="0dp"
        android:layout_weight="1" />

I also had to add this to onResume in the DialogFragment:

@Override
public void onResume() {
    super.onResume();
    getDialog().getWindow().setLayout(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.WRAP_CONTENT);
}
like image 83
Sam Stern Avatar answered Oct 19 '22 17:10

Sam Stern


This seems to be a problem with layouts and dialog fragments. Set it manually instead:

@Override
public void onResume() {
    super.onResume();
    Window window = getDialog().getWindow();
    window.setLayout(your_value, your_value);
    window.setGravity(Gravity.CENTER);
}

If you need to wrap the content in the fragment, you can traverse the views and sum up the total height and then use that as the layout height.

like image 4
SoroushA Avatar answered Oct 19 '22 17:10

SoroushA