3

I am using my own subclass of Dialog and trying to build it in such a way that if there is not enough content to fill the screen, it will shrink to fit the content (i.e. wrap_content). I can do this with the following layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg">

    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:background="@drawable/dialog_title_bg">

        . . .

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/content"
        android:orientation="horizontal">

        . . .

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_container">
    </FrameLayout>
</RelativeLayout>

(I've removed the bits that - I think - are not important). This works great when the content doesn't fill the screen:

But when the content is big, it pushes the buttons off the bottom:

A slight adjustment, I change the button_container so it's got layout_alignParentBottom="true" and the content is above it, like so:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/dialog_bg">

    <RelativeLayout
        android:id="@+id/title_container"
        android:layout_width="match_parent"
        android:layout_height="60dp"
        android:gravity="center_vertical"
        android:background="@drawable/dialog_title_bg">

        . . .

    </RelativeLayout>

    <LinearLayout
        android:id="@+id/button_container"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:orientation="horizontal">

        . . .

    </LinearLayout>

    <FrameLayout
        android:id="@+id/content"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/title_container"
        android:layout_above="@+id/button_container">
    </FrameLayout>
</RelativeLayout>

Now it works for when the content is too big for the screen:

But when the content does not fill the screen, the dialog still does:

How can I get the best of both worlds? I could probably do it in code by setting the "maxHeight" to the height of the screen minus the dialog border and the height of the title and buttons, but that seems a bit hacky to me. I tried looking at the implementation of AlertDialog (which seems to handle this situation correctly) but it looked like black magic to me...

Edit: as requested, here is the content of the layout file that I add to the FrameLayout that represents the "content" of the dialog:

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

    <ListView
        android:id="@+id/report_items"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true" />

</RelativeLayout>
Dean Harding
  • 71,468
  • 13
  • 145
  • 180

1 Answers1

0

Instead of frame layout, better to use the linear. It will work.. If u post the xml code then I can check..

mainu
  • 448
  • 2
  • 11
  • `FrameLayout` is used because that's just a container for whatever content I want to actually put in the dialog (i.e. the list and the message in the two example screenshots). I tried with a `LinearLayout` but it didn't make a difference. In my example, the list view is literally just a `ListView` (inside a `RelativeLayout`) which I inflate and add to the `FrameLayout` in my dialog's `onCreate` implementation. FWIW, I'll update the question with the contents of that layout as well... – Dean Harding Dec 20 '12 at 07:33
  • I've updated the question with the layout I add to that `FrameLayout`. As you can see, there's no padding or margins. – Dean Harding Dec 20 '12 at 07:42