In an alert dialog, how can I reduce the spacing between the 'message' and my view?

Viewed 175

For example, the following code:

AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle("foo");
builder.setMessage("bar");
EditText edit = new EditText(activity);
builder.setView(edit);
builder.show();

Results in this dialog

As can be seen, there is a large vertical space between the message and the edit text. I would like to reduce it, ideally without having to use a custom layout.

1 Answers

If we display the AlertDialog in an emulator and show the component tree in the Layout Inspector, we see that the extra space comes from a Space widget called "textSpacerNoButtons"

enter image description here

and a FrameLayout named "customPanel" that holds the EditText

enter image description here

The Space widget has a specified height of 18dp (which we shall see in a moment) while the FrameLayout has a minimum height of 48dp which can be seen in the attributes of the Layout Inspector.

The layout used by AlertDialog is alert_dialog_material.xml. I have copied the relevant portions below:

<FrameLayout
    android:id="@+id/contentPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <ScrollView
        android:id="@+id/scrollView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:clipToPadding="false">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <Space
                android:id="@+id/textSpacerNoTitle"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />

            <TextView
                android:id="@+id/message"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:paddingEnd="?attr/dialogPreferredPadding"
                android:paddingStart="?attr/dialogPreferredPadding"
                style="@style/TextAppearance.Material.Subhead" />

            <Space
                android:id="@+id/textSpacerNoButtons"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="@dimen/dialog_padding_top_material" />
        </LinearLayout>
    </ScrollView>
</FrameLayout>

<FrameLayout
    android:id="@+id/customPanel"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="48dp">

    <FrameLayout
        android:id="@+id/custom"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</FrameLayout>

For the Space widget, the height is defined as @dimen/dialog_padding_top_material which is 18dp in the example. Changing this value has other implications, so we will leave it alone.

So, that is the source of the space but how to fix it? The way I would prefer is to use a custom view even if it is a near exact copy of "alert_dialog_material.xml". If that is not acceptable, then we must make changes on-the-fly to remove the minimum height of contentPanel and to set the Space widget's visibility to gone.

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("foo");
builder.setMessage("bar");
EditText edit = new EditText(this);
builder.setView(edit);
AlertDialog alertDialog = builder.show();
View decorview = alertDialog.getWindow().getDecorView();
View view = decorview.findViewById(R.id.contentPanel);
view.setMinimumHeight(0);
view = decorview.findViewById(R.id.textSpacerNoButtons);
view.setVisibility(View.GONE);

enter image description here

Related