Anthony
Anthony

Reputation: 7828

Android Dialog Height

I'm trying to setup a simple dialog, but I can't seem to control the height. It's always max screen height. Is there some way to get it to properly wrap to the contents?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/save"
        android:id="@+id/buttonSave"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:textAlignment="center"
        style="?attr/borderlessButtonStyle"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@android:string/cancel"
        android:id="@+id/buttonCancel"
        android:layout_alignParentBottom="true"
        android:layout_toStartOf="@+id/buttonSave"
        style="?attr/borderlessButtonStyle"/>

</RelativeLayout>

.

final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.save_dialog);
dialog.setTitle(R.string.saveAs);
dialog.show();

Upvotes: 0

Views: 236

Answers (1)

Patrick
Patrick

Reputation: 1075

You have android:layout_alignParentBottom="true" in your XML. This forces your Button to be positioned at the very bottom of the space it has available to it, effectively making the RelativeLayout have a match_parent layout_height behavior instead of a wrap_content one.

An alternative would be to use a LinearLayout or a GridLayout instead. Something along the lines of this would do the trick:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <!-- 
    The rest of your layout can go here 
    with a layout_height of 0dp and a layout_weight of 1
    if you want your buttons to dock themselves at the bottom of the dialog
    -->
    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@android:string/cancel"
            android:id="@+id/buttonCancel"
            style="?attr/borderlessButtonStyle"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/save"
            android:id="@+id/buttonSave"
            android:textAlignment="center"
            style="?attr/borderlessButtonStyle"/>
    </LinearLayout>
</LinearLayout>

Upvotes: 3

Related Questions