I have 3 relative layouts (lets call them viewgroups) and layed out like this
VIEWGROUP 1
VIEWGROUP 2
VIEWGROUP 3
I want VIEWGROUP 1 and VIEWGROUP 3 to take the space they need to display their contents. Then whatever space left, is allocated to ViewGroup2 (and its contents will be scrollable). Currently I have a Root Relative layout that add each group with the "layout_below" attribute in reference to the previous one.
The problem that when VIEWGROUP 2 (say its textview component) is large, then it pushes VIEWGROUP3 out of the screen.
How do you recommend fixing this? I know there is a "hardcoding" way of fixing by assigning specific layout height but I want to avoid hardcoding and apply "use whatever space left"
Thanks
Best How To :
It seems you have two different issues.
Issues 1: Assigning extra space to 3rd view
Solution: Use LinearLayout
as parent and apply layout_weight
to 3rd child view
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!-- LinearLayout height must match_parent -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</RelativeLayout>
</LinearLayout>
Issue 2: First two layouts pushing 3rd off screen.
Solution a: Make sure first two layouts smaller (don't know the details may not be an option)
Solution b: Scroll entire screen (simplest solution IMO)
T do this you do not want to use weight_sum
as posted above. Instead wrap everyting in a ScrollView
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"><!-- Important ScrollView height match paternt -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"><!-- Important child view height wrap content -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp">
</RelativeLayout>
</LinearLayout>
</ScrollView>
EDIT In response to your comment you can make the middle view only scroll. This will work but has a couple of potentail issues.
- The middle view will not expand to fill the empty space.
If your top or bottom views get to large it will be hard to scroll.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"><!-- height 0dp with layout_weight -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"><!-- height wrap_content -->
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
Or if your prefer to use RelativeLayouts
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"><!-- height match_parent -->
<RelativeLayout
android:id="@+id/layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<RelativeLayout
android:id="@+id/layout_3"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
<ScrollView
android:layout_above="@id/layout_3"
android:layout_below="@id/layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="@+id/layout_2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>