0

I have an AlertDialog with a ConstraintLayout as a view and all of the children have a height of 0dp, i.e match constraints:

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:maxHeight="400dp">

    <TextView
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

    <!-- Other children -->

I want the dialog to take as much height as possible on screen, to a maximum of 400dp. However the layout above produces a dialog with 0 height, invisible.

I tried using dialog.getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT). The dialog took the whole screen height but the ConstraintLayout was still invisible.

Is there any way to do this?

Nicolas
  • 6,611
  • 3
  • 29
  • 73
  • 0dp is not MATCH_PARENT, MATCH_PARENT is, 0dp is only usefull at creation if you use LinearLayout with a weight attribute. Why don't you give the text a height of 400? else to increase the dialog you need to override WindowParams on Dialog creation (by extending it or implementing on showDialog/onPrepareDialog of a Activity) – Marcos Vasconcelos Mar 09 '18 at 21:27
  • If you don't know how a ConstraintLayout works, please don't answer. `match_parent' can't be used with it and `0dp` means match constraint and nothing else. – Nicolas Mar 09 '18 at 21:42
  • All right it goes against Android usual patterns, if you dont consider the other part of the comment dont mind asking. – Marcos Vasconcelos Mar 09 '18 at 21:53
  • The problem is that when I increase the dialog's height with layout params (in the dialog's onShowListener), the dialog effectively takes all available space but the ConstraintLayout is still invisible. – Nicolas Mar 09 '18 at 22:03
  • @MarcosVasconcelos You can take a look at my answer, I don't if that was what you had in mind, but it works perfectly. – Nicolas Mar 10 '18 at 20:52
  • Yes, it was. Theres more ways to achieve it but all works like the same. – Marcos Vasconcelos Mar 12 '18 at 14:01

1 Answers1

0

I found a solution after diving deep into Android's source code. The code below finds the maximum dimensions that the dialog can take and compares it with my own maximum dimensions. After that I set the size of the dialog and the size of my view manually.

private int dialogMaxWidth = 1200;  // don't do that
private int dialogMaxHeight = 1200;

@Override
public @NonNull Dialog onCreateDialog(Bundle state) {
    View view = LayoutInflater.from(context).inflate(R.layout.my_dialog, null);

    final Dialog dialog = new Dialog(context);
    dialog.setOnShowListener(new DialogInterface.OnShowListener() {
        @Override
        public void onShow(DialogInterface dialogInterface) {
            // Get maximum dialog dimensions
            // Basically (screen size) - (dialog's drawable padding)
            Rect fgPadding = new Rect();
            dialog.getWindow().getDecorView().getBackground().getPadding(fgPadding);
            DisplayMetrics metrics = context.getResources().getDisplayMetrics();
            int height = metrics.heightPixels - fgPadding.top - fgPadding.bottom;
            int width = metrics.widthPixels - fgPadding.top - fgPadding.bottom;

            // Set dialog's dimensions
            if (width > dialogMaxWidth) width = dialogMaxWidth;
            if (height > dialogMaxHeight) height = dialogMaxHeight;
            dialog.getWindow().setLayout(width, height);

            // Set dialog's content
            view.setLayoutParams(new ViewGroup.LayoutParams(width, height));
            dialog.setContentView(view);
        }
    });

    return dialog;
}

Note: context can be replaced with getActivity().

Nicolas
  • 6,611
  • 3
  • 29
  • 73