1

I have a dialog with this content:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ScrollView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="25dp" >
        <LinearLayout
           ...
        </LinearLayout>
    </ScrollView>

    <Button
        android:id="@+id/closebtn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/scrollview"
        android:layout_centerHorizontal="true" />

</RelativeLayout>

I want the dialog to be sized vertically to fit as much of the content of the LinearLayout as it can. However, I'm finding that, if the contents of the LinearLayout are too tall, the ScrollView fills the dialog and the close button gets pushed past the bottom and is not visible.

One thing I tried was to make the Button layout_alignParentBottom="true" and make the ScrollView layout_above="@+id/closebtn", but then the dialog always stretches to fill the whole screen vertically, even if the content of the LinearLayout is really short.

Travis
  • 2,961
  • 4
  • 22
  • 29

3 Answers3

0

One solution to this is to programatically get the height of the screen and specify the height of your scroll view manually.

To get screen dimensions (in pixels):

Get screen dimensions in pixels

The issue is that the height of your button will be a different number of pixels on each screen, based on pixel density, yet your button will be a set amount of DP.

In your XML, set a height for your button in DP.

Then calculate how many pixels high your button is. See: What is the difference between "px", "dp", "dip" and "sp" on Android?

You can then use the height of the screen, minus the height of your button, to determine the size you can set in your button's layout params.

LayoutParams params = findViewById(R.id.your_scroll_view).getLayoutParams();
params.height = screenHeight - buttonHeight;
Community
  • 1
  • 1
WindyB
  • 786
  • 3
  • 9
  • I don't think this would size the dialog down to fit just the contents if they can all fit within the screen size. – Travis Jun 25 '12 at 06:20
0

I ended up changing my RelativeLayout to a LinearLayout to get this working. I didn't do that originally because when I tried it it gave my a really tall and skinny dialog (the inner LinearLayout contains ImageViews and TextViews). But, surprisingly, changing all my views from width=fill_parent to wrap_content caused the dialog to stretch horizontally to fill the screen, which is what I wanted, and the height behaves correctly.

Travis
  • 2,961
  • 4
  • 22
  • 29
0

after you called:

AlertDialog dialog = builder.show();    

then call:

fixScrollViewHeight(scrollView);

//

private void fixScrollViewHeight(ScrollView scrollView) {
    int screenHeight = activity.getWindowManager().getDefaultDisplay().getHeight();
    LayoutParams lp = scrollView.getLayoutParams();
    lp.height = screenHeight / 3;
    scrollView.setLayoutParams(lp);
}
fantouch
  • 1,159
  • 14
  • 12