Azartys
Azartys

Reputation: 543

AppCompatDialogFragment with ConstraintLayout dialog width

I'm trying to have a AppCompatDialogFragment with a root layout as a ConstraintLayout.

The root layout have a layout_width set to wrap_content. But when I execute the code, the dialog is shrink have a fixed width even if the content change.

Here's the layout code generated

<?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"
                                             xmlns:tools="http://schemas.android.com/tools"
                                             android:layout_width="wrap_content"
                                             android:layout_height="wrap_content"
                                             android:orientation="vertical"
  >

  <TextView
    android:id="@+id/textView5"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="16dp"
    android:layout_marginEnd="16dp"
    android:layout_marginStart="16dp"
    android:layout_marginTop="16dp"
    android:text="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Cras tempus tristique ornare. Aenean ut magna in diam aliquet accumsan. "
    android:textSize="36sp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>
</android.support.constraint.ConstraintLayout>

Here's how i inflate the layout:

View view = inflater.inflate(R.layout.weather_dialog, container, false);

Long text

Short text

We can see that with a long text the dialog hit some sort of "max width" (even without the attribute).

How can i have a dialog that wrap its content in width without this "limit" ?

Upvotes: 3

Views: 2961

Answers (1)

B&#246; macht Blau
B&#246; macht Blau

Reputation: 13019

There is a pair of Window style attributes named android:windowMinWidthMajor and android:windowMinWidthMinor.

The minimum width the window is allowed to be, along the major [minor] axis of the screen

(quoted from documentation)

If you use a Theme for your Dialog by defining some style

<style name="FragmentDialogStyle" parent="Theme.AppCompat.Dialog">
    <item name="windowMinWidthMajor">@dimen/dialog_fragment_width</item>
    <item name="windowMinWidthMinor">@dimen/dialog_fragment_width</item>
    <item name="android:windowMinWidthMajor">@dimen/dialog_fragment_width</item>
    <item name="android:windowMinWidthMinor">@dimen/dialog_fragment_width</item>
</style>

and setting

<item name="android:dialogTheme">@style/FragmentDialogStyle</item>
<item name="dialogTheme">@style/FragmentDialogStyle</item>

in the application Theme, then the Dialog width will be precisely dialog_fragment_width if you also use

android:layout_width="0dp"

with your TextView.

Whereas if you keep using

android:layout_width="wrap_content"

the "max width" you observed will restrain your Dialog width.

By the way, if you color the backgrounds of the TextView and the ConstraintLayout, you will observe that only when using the 0dp width value all the margins are correct.

So it seems that your best option is to set android:layout_width="0dp" for the TextView and to provide different values for dialog_fragment_width, depending on the current screen (for Nougat+: app window) width. For this, you'll need different directories like res/values-w600dp.

Please note: I tested with emulators for a tablet running Marshmallow and a phone running Lollipop. Although it worked, I'd be happier if I also could explain why. So I think this is more like half an answer but I wanted to share my observations.

Upvotes: 3

Related Questions