androidandroid-layoutandroid-relativelayout

What am I misunderstanding about aligning views at the bottom?


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 do I 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?


Solution

  • 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