17

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>
Aidan Laing
  • 1,260
  • 11
  • 23

2 Answers2

38

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!

Piyush Kumar
  • 623
  • 7
  • 11
  • 2
    Note that this doesn't work when added in `onCreateView()`. I had to add it in `onResume()`. – Mateus Gondim Mar 16 '18 at 21:27
  • I am using dialog style for my activity theme and after setting this, the margin is gone, so I need to re-add the left and right margin again – HendraWD May 14 '18 at 06:28
  • i have using a constraintlayout in a dialog and shows wrong. putting this to the dialog seems better, but why. why this works. I must putt this in every contraint dialog? – Roger Jun 12 '20 at 00:07
0

It seems to be working if you set a width on the parent ConstraintLayout instead of using match_parent. That's the best solution I was able to find.

Aidan Laing
  • 1,260
  • 11
  • 23