I try my best to make the layout look right, and I tried to create the shortest and smallest possible example of my problem.
My goal is to have a header and footer at the top and bottom of the screen with a ListView between, with a different view (call it a label, this is a gray square from the screen snapshots) immediately below the ListView . This label and footer should always be displayed when the ListView needs to scroll.
Visual result
If the ListView does not need to scroll (this is correct):

When you need to scroll through the ListView , the footer and the gray window are forced out of the screen (wrong):

Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="header" android:padding="20dp" android:textSize="18sp" android:background="@color/red"/> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/list" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll" android:padding="5dp" android:background="@color/light_gray" android:textColor="@color/black"/> <View android:layout_width="0dp" android:layout_height="0dp" android:layout_weight="1"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="footer" android:padding="20dp" android:textSize="18sp" android:background="@color/blue"/> </LinearLayout>
Testing
public class TestActivity extends ListActivity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ArrayList<String> items = new ArrayList<String>(); items.add("one"); items.add("two"); items.add("three"); items.add("four"); items.add("five"); items.add("six"); items.add("seven"); items.add("eight"); items.add("nine"); items.add("ten"); setListAdapter(new ArrayAdapter<String>(this, R.layout.simple_list_item_1, items)); setContentView(com.myproject.android.R.layout.test); } }
I tried several different approaches, for example, giving ListView layout_weight="1" and removing the empty View , which I use to push the footer from the bottom. This is almost what I want, it keeps the footer and label visible when the ListView scrolls, but when it has only 1 or 2 elements, I need a gray window right below the ListView . I also tried using RelativeLayout without success. I guess I totally donโt understand.
EDIT
Here is my attempt with RelativeLayout , which is still incorrect.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="header" android:padding="20dp" android:textSize="18sp" android:background="@color/red" android:id="@+id/header" /> <ListView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@android:id/list" android:layout_below="@id/header"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="this should be directly below the ListView, but not pushed off screen when the ListView needs to scroll" android:padding="5dp" android:background="@color/light_gray" android:textColor="@color/black" android:layout_below="@android:id/list"/> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="footer" android:padding="20dp" android:textSize="18sp" android:background="@color/blue" android:layout_alignParentBottom="true" android:id="@+id/footer"/> </RelativeLayout>
Relative layout (still wrong):

wsanville
source share