0

I tried to align my linearlayout at the bottom of my entire layout which is relative, and found the solution here that does it: How to align views at the bottom of the screen?

But why wouldn't this work also?

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.sandbox.activities.ListActivity">

<ListView
    android:id="@+id/my_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />

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

    <EditText
        android:id="@+id/my_edittext"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:hint="Enter Search Term" />

    <Button
        android:id="@+id/find_button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:text="Find"
        android:onClick="findThings"/>

</LinearLayout>

If the listview turns out to be short, like 1 row, "search_footer" is just underneath it, and there is a huge gap below it.

1) "search_footer" is told to align at the bottom of the parent, which is the relativelayout. Since the relativelayout's height is match_parent, and it's the root layout, this means the whole height of the screen, so why wouldn't "search_footer" be at the bottom of the screen no matter the size of the listview?

2) Also, if I change the search_footer's height to match_parent, I expected a really tall button, but it remained unchanged-why?

Community
  • 1
  • 1
HukeLau_DABA
  • 2,384
  • 6
  • 33
  • 51

2 Answers2

1

Add to ListView this attribute

android:layout_above="@id/search_footer"

and remove

android:layout_below="@id/my_listview"

attribute from LinearLayout.

Onik
  • 19,396
  • 14
  • 68
  • 91
0

You specified that search_footer align to bottom of parent but also to be below the listview, so in the case that the list is short the search footer just stretches in the space between the listview and the bottom of the screen. you want to specify the footer to be aligned to the bottom of the parent and the listview to be above it:

<ListView
    android:id="@+id/my_listview"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:layout_above="@+id/search_footer"
    />

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

When you change the height of the search_footer the buttons doe't change because they are set to android:layout_height="wrap_content". Your layout would normally change when you do this however in this case it didn't change either, due to the same reason as above

Code-Apprentice
  • 81,660
  • 23
  • 145
  • 268
Or Bar
  • 1,566
  • 11
  • 12
  • With `android:layout_below="@id/my_listview"` he'll get `Circular dependencies` error in xml file. – Onik Aug 17 '14 at 01:00
  • @Onik I think OrBar made a copy-paste error and forgot to remove the reference to the list view. – Code-Apprentice Aug 17 '14 at 01:02
  • With regards to the footer height it sounds like what you're saying is that deeper nested layout settings override their parents? While I see your solution working for the footer, I still don't get why saying the footer goes below the listview and the listview goes above the footer are different; In both cases the parent is relativelayout whose height is the full screen via match_parent – HukeLau_DABA Aug 17 '14 at 01:20
  • @HukeLau_DABA The following are just my thoughts. Saying parent's height with match_parent attr takes the full screen's height isn't a solid rule. What if the elements don't fit to the height? How would graphical preview calculate the element's heights in advance? I guess, that's the point for the result you got. When "saying the footer goes below the listview" how to calculate the list's height? But when "saying listview goes above the footer" you know exactly that its height is between the top of the screen and the footer... – Onik Aug 17 '14 at 01:44
  • @Onik, yes that was a copy paste error. The difference between the footer goes below the listview and the listview goes above the footer comes down to which of the two is aligned first and which one will align itself based on the other. Basically you want the more important (layout wise) to anchor to the bottom, and the listview to stretch to fit in the space above it, rather than the listview to take up the space it needs and the footer to stretch to fit the area blow it. – Or Bar Aug 17 '14 at 01:49