Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I place widgets above and below a listview?

I have a list view and want to put stuff both above(on the y axis) and below(y axis) it including images, text views, and a row of buttons. Below is a simplified version of what I am creating. Unfortunately the list covers(i.e. above on the z axis) the header so the header text is not visible instead of being underneath (on the y axis)

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

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true"
        />

    <ListView  android:id="@+id/list_view" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:layout_above="@id/footer"
        android:background="#ff9999ff"/>

    <TextView android:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentTop="true"
        android:layout_above="@id/list_view"
        android:baselineAlignBottom="false"
        />

</RelativeLayout>

Here is the corresponding Activity class:

public class SampleListActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.list_activity);

    }
}
like image 975
Jay Askren Avatar asked Mar 16 '10 12:03

Jay Askren


People also ask

How do I put widgets from the top of the screen flutter?

How to place text widgets one at top and other at bottom. If there are only two widgets in your Column widget, then you can set MainAxisAlignment attribute to MainAxisAlignment. spaceBetween. You have to set this inside your Column Widget, above or below the children attribute.

Is a ListView scrollable?

Is ListView scrollable by default? scrollable Boolean|String (default: false) If set to true the listview will display a scrollbar when the content exceeds the listview height value. By default scrolling is disabled. It could be also set to endless in order to enable the endless scrolling functionality.


3 Answers

I couldn't get relative layout to work in Android 1.5 but I did get Linear Layout to work. Below is my solution:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="wrap_content"
  android:layout_height="fill_parent"
  android:orientation="vertical">

    <TextView android:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_weight="0"
        />
    <ListView  android:id="@+id/list_view" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:background="#ff9999ff"
        android:layout_weight="1"/>

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_weight="0"
        />
</LinearLayout>
like image 169
Jay Askren Avatar answered Sep 18 '22 00:09

Jay Askren


This should work using RelativeLayout:

<?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:id="@+id/header" 
        android:text="header"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" />

    <TextView android:id="@+id/footer" 
        android:text="footer"
        android:layout_height="wrap_content" 
        android:layout_width="fill_parent" 
        android:layout_alignParentBottom="true"/>

    <ListView android:id="@id/android:list" 
        android:layout_height="fill_parent" 
        android:layout_width="fill_parent"
        android:layout_below="@id/header"
        android:background="#ff9999ff"/>

</RelativeLayout>

Edit: removed android:orientation as suggested

like image 42
JoséMi Avatar answered Sep 21 '22 00:09

JoséMi


If you are targeting Android 1.6 and higher, add an android:layout_below="@id/header" to your existing attributes on the ListView.

If you are still targeting Android 1.5, you will need to make that android:layout_below="@+id/header" and perhaps remove the + sign from the current android:id="@+id/header" attribute in the TextView.

RelativeLayout, as of 1.6, supports forward-references to other widgets, so long as the first occurrence of the ID value has the + sign.

like image 28
CommonsWare Avatar answered Sep 21 '22 00:09

CommonsWare