views:

25

answers:

1

One part of my layout is supposed to have two components: A ScrollView on top, and a MapView on the bottom. Both views should be exactly half the size of the parent.

I have the size of the parent (I'm switching to this layout at a point when I already have the size), but I don't know how to define the maximum size. Besides, I figure there is a solution that doesn't require defining the size; for example by having something like a Swing-style GridLayout, or defining a weight somewhere, but I haven't found anything that sounds like it would work.

My current approach is this here, which defines the minimum size, which obviously falls apart as soon as the ScrollView grows beyond half the height of the parent:

The layout (the parent is a LinearLayout):

<LinearLayout
    android:id="@+id/result_layout"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:visibility="gone"
    >

<ScrollView
    android:id="@+id/result_scroll"
    android:layout_height="fill_parent"
    android:layout_width="fill_parent"
    android:orientation="vertical"
    >

<LinearLayout
    android:id="@+id/result"
    android:layout_height="fill_parent"
    android:layout_width="wrap_content"
    android:paddingRight="?android:attr/scrollbarSize"
    android:orientation="vertical"
/>

</ScrollView>

<view class="com.google.android.maps.MapView"
    android:id="@+id/mapview"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content"
  />

</LinearLayout>

And the code, after I switch to it (totalWidth being the width of the parent, viewHeight half the height of the parent):

LinearLayout.LayoutParams lp =
            new LinearLayout.LayoutParams(totalWidth, viewHeight);
scrollView.setLayoutParams(lp);
+2  A: 

One part of my layout is supposed to have two components: A ScrollView on top, and a MapView on the bottom. Both views should be exactly half the size of the parent.

Put them in a LinearLayout. Set the the height of each to 0px. Set the weight of each to 1.

For example:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent">
<ScrollView
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"/>
<Button
    android:layout_width="fill_parent"
    android:layout_height="0px"
    android:layout_weight="1"/>
</LinearLayout> 

Besides, I figure there is a solution that doesn't require defining the size...or defining a weight somewhere

If you don't tell it how big it is explicitly ("defining the size") or implicitly ("defining a weight"), how are you expecting to know it is supposed to be "exactly half the size of the parent"?

CommonsWare
I knew that Mark is up for the task! No layout problem too big. AFAIU, most Android layouts operate under the concept of "minimum height", so after measuring, a view will either get the measured height, or the entire parent's height if using fill_parent.
EboMike
As for your second question - the comma was to separate examples, i.e. "I figure there is a solution that doesn't require defining the size: [examples start here] Using a GridLayout, or defining a weight". Sorry, that was ambiguous.
EboMike
Btw, I tried it, and it kind of works, but there is no minimum height. The ScrollView initially has a very small height, so the map takes up most of the screen. As the ScrollView grows, it takes up more space but never exceeds half of the height, which is good. I'll accept your answer, but do you have any idea on how to make the minimum size half of the parent?
EboMike
@EboMike: "The ScrollView initially has a very small height, so the map takes up most of the screen." -- see the example I added above. The `ScrollView` and the `Button` each take up 50% of the screen. This holds true regardless of whether the `ScrollView` is empty or bigger than the screen.
CommonsWare
I'm a schmuck. The parent LinearLayout had wrap_content as its height. Now it's perfect. Thanks again, Mark, you're the master of anything layout-related!
EboMike