Android RelativeLayout and height of wrap_content?
Asked Answered
N

2

6

I am trying to make a selection ListActivity, similar to the one used to add shortcuts to the launcher screens. I have rolled my own header and footers, which I would like to be "sticky" at the top and bottom of the view when on screen. In order to do this, I am using a RelativeLayout with the header set to dock to top, footer set to dock to bottom, and the list set to go below the header and above the footer. In terms of the overall layout of the activity, this is rendering as I would expect. The header is sticky to the top, the footer is sticky to the bottom, and the list scrolls in between them.

One odd thing though happened when I switched to the RelativeLayout as my root. Please see the following screenshot:

enter image description here

I want my Activity's height to be wrap_content, so that the form is only as high as the content displayed in it, but once i switched to RelativeLayout, it seems to render the Activity effectively as fill_parent, taking up the whole screen, even though the content doesn't warrant it. Notice that there are not enough list items to fill the screen, which with the fill_parent style, is leaving a bunch of whitespace between the end of the list, and the footer. I was setting my height's via styles - which worked fine with LinearLayout, but seems to be ignored now. So I tried hard-coding the height directly on the RelativeLayout, but it still doesn't work and still renders as fill_parent.

Here is my layout code:

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
                    style="@style/GroupsList"  
                    android:layout_height="wrap_content">

        <FrameLayout android:layout_height="wrap_content" 
                     android:layout_width="fill_parent" 
                     android:id="@+id/hdrGroups" 
                     android:layout_alignParentTop="true">
            <include layout="@layout/title" 
                     android:layout_width="fill_parent" 
                     android:layout_height="wrap_content">
            </include>
        </FrameLayout>

        <FrameLayout style="@style/MyFooter" 
                     android:layout_height="wrap_content" 
                     android:layout_width="fill_parent" 
                     android:layout_alignParentBottom="true" 
                     android:id="@+id/ftrGroups">
            <ImageView style="@style/CloseButton" 
                       android:layout_height="wrap_content" 
                       android:layout_width="wrap_content" 
                       android:src="@drawable/add" 
                       android:id="@+id/imgGroupsAdd" 
                       android:clickable="true">
            </ImageView>
        </FrameLayout>

        <ListView android:divider="#9f9f9f" 
                  android:id="@+id/android:list" 
                  android:choiceMode="singleChoice" 
                  android:dividerHeight="1dp" 
                  android:layout_height="wrap_content" 
                  android:layout_width="fill_parent" 
                  android:layout_below="@id/hdrGroups" 
                  android:layout_above="@id/ftrGroups">
        </ListView>

        <TextView android:text="@string/browser_no_groups" 
                  style="@style/ListedItemB" 
                  android:id="@+id/android:empty" 
                  android:layout_height="wrap_content" 
                  android:layout_above="@id/ftrGroups" 
                  android:layout_below="@id/hdrGroups" 
                  android:layout_width="fill_parent">
        </TextView>
    </RelativeLayout>

All layout is done via XML, ... I am not doing any layout in code. How can I get the sticky header and footer while also having the activity as a whole behave in a wrap_content mode for its height? Is there some other way I should be going about this instead of a RelativeLayout?

Nananne answered 11/5, 2011 at 5:21 Comment(3)
Are you able to post a more complete code example to replicate the screenshot and the problem? My friend and I have an idea, but we're too lazy to put together the missing pieces you haven't provided.Expertize
I'll see if I can throw something together in the next couple days.Nananne
Well, I finally got a chance and threw an example... find it here: dl.dropbox.com/u/19767586/layout.test.zip . I also included two screenies, one is what it does look like, one is what it should look like.Nananne
S
26

According to the developer documentation your desired behaviour is not possible for Relative layout ;

"Note that you cannot have a circular dependency between the size of the RelativeLayout and the position of its children. For example, you cannot have a RelativeLayout whose height is set to WRAP_CONTENT and a child set to ALIGN_PARENT_BOTTOM."

RelativeLayout

To solve your problem you could maybe try to use a linear layout, set to wrap_content and set max height via code to screen height.

You can get the screen height as described here : get screen height

Spense answered 29/7, 2011 at 13:52 Comment(3)
Drat! Oh well... I already rewrote with a different layout all around in the meantime, but this sure would be nice. :-?Nananne
I have exactly the same problem. Android fragmentation at its best. The LinearLayout will screw the footer of the layout making it squeezing to zero at the bottom if the list is long enough. So sad.Jeannettejeannie
I wanted to put another view above the bottom view, but I faced this problem, so I did it reverse order: set the top view to 'ALIGN_PARENT_TOP' (escape from the problem) and set the bottom view with 'layout_below'. My problem then fixedTricorn
P
1

I just change my RelativeLayout in FrameLayout and all starts to work

Project answered 28/11, 2016 at 17:36 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.