Using ConstraintLayout with custom Android Dialog

Aidan Laing picture Aidan Laing · Sep 21, 2017 · Viewed 7.5k times · Source

I'm creating a custom Dialog by extending Dialog and using setContentView(). The problem is when I try to show the Dialog it only shows the background shadow instead of my custom layout. I've managed to get the custom layout to show by wrapping it in a RelativeLayout, but I'm wondering if there is a better solution or something I'm doing wrong. Using constraintLayout version 1.0.2. Thanks!

Here is my Custom Dialog:

class EventsFilterDialog(context: Context): Dialog(context) {
    init {
        setContentView(R.layout.dialog_events_filter)
        setCancelable(true)
    }
}

Here is a simplified version of my R.layout.dialog_events_filter that has the exact same result:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:background="@color/white"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <TextView
        android:id="@+id/headerTv"
        android:text="@string/events_filters"
        android:textColor="@color/white"
        android:textSize="18sp"
        android:textStyle="bold"
        android:gravity="center"
        android:background="@color/colorPrimaryLight"
        android:padding="16dp"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <View
        android:id="@+id/middleView"
        android:layout_width="0dp"
        android:layout_height="256dp"
        app:layout_constraintTop_toBottomOf="@+id/headerTv"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent" />

    <TextView
        android:id="@+id/closeTv"
        android:text="@string/close"
        android:textColor="@color/white"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:gravity="center"
        android:background="@color/colorPrimaryLight"
        android:layout_width="0dp"
        android:layout_height="48dp"
        app:layout_constraintTop_toBottomOf="@+id/middleView"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toLeftOf="@+id/applyTv" />

    <TextView
        android:id="@+id/applyTv"
        android:text="@string/apply_filter"
        android:textColor="@color/white"
        android:textAllCaps="true"
        android:textStyle="bold"
        android:gravity="center"
        android:background="@color/colorAccent"
        android:layout_width="0dp"
        android:layout_height="48dp"
        app:layout_constraintTop_toBottomOf="@+id/middleView"
        app:layout_constraintLeft_toRightOf="@+id/closeTv"
        app:layout_constraintRight_toRightOf="parent"/>

</android.support.constraint.ConstraintLayout>

Answer

Piyush Kumar picture Piyush Kumar · Dec 8, 2017

Set LayoutParams of window of the dialog at run time. This one worked for me.

dialog.setContentView(R.layout.dialog_select_location);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);

Best of luck!