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>
ConstraintLayout has flat view hierarchy unlike other layouts, so does a better performance than relative layout. Yes, this is the biggest advantage of Constraint Layout, the only single layout can handle your UI.
Most of what can be done in LinearLayout and RelativeLayout can now be done with a new layout system called ConstraintLayout. It's important to note the class hierarchy of these View Layouts.
A ConstraintLayout is a ViewGroup which allows you to position and size widgets in a flexible way. Note: ConstraintLayout is available as a support library that you can use on Android systems starting with API level 9 (Gingerbread).
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!
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