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?
This method is deprecated. androidx.
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.
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.
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.
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);
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With