0
votes

I have created a listview adding textview to header and footer .Its visible only when you scroll to the top and bottom of listview.I would like that to be floating.How do I do that ?I really appreciate any help .Thanks in Advance.

          View header = getLayoutInflater().inflate(R.layout.header, null);

          list.addHeaderView(header);
3
I wrap my listview in a linearlayout that's wrapped in a relativelayout. that way i can have textviews and/or buttons on the top or bottom and the list scrolls between them. I do it in XML if you'd like the code.Bill Gary
yup as Commonsware said.jason
@Bill Gary: I wrap my listview in a RelativeLayout (no need for an extra LinearLayout). That way I can have textviews and/or buttons on the top or bottom and the list scrolls between them. I do it in XML. No joke - I always do so. Also with ExpandableListViewsPhantômaxx
can you share the xml layout please.THanks for your time.jason

3 Answers

2
votes

I would like that to be floating.How do I do that ?

If by "floating" you mean "fixed, not moving, above and below the ListView", the solution is to not put them in the ListView. Use a LinearLayout to position your header and footer just above and just below the ListView itself.

If you have some other definition of "floating", please edit your question to explain more, and I can delete this answer.

1
votes
<?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">

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_above="@+id/linearLayout1"
    android:layout_below="@+id/tvListHeader"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <TextView
        android:id="@android:id/empty"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/no_notes"/>
</LinearLayout>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:gravity="center"
    android:orientation="horizontal" >

    <Button
        android:id="@+id/bListBack"
        android:layout_width="200dp"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Back" />
</LinearLayout>

<TextView
    android:id="@+id/tvListHeader"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_marginBottom="10dp"
    android:gravity="center"
    android:text="Text"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:textColor="#FF0000"
    android:textStyle="bold" />

1
votes

So, if by "floating" you mean fixed, here is my way to do that:

<?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/txtQuestion"
        android:background="@color/blue_gray_semi"
        android:layout_alignParentTop="true"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:minHeight="48dp"
        android:padding="8dp"
        android:textColor="@color/white"
        android:textSize="16sp"
        android:textStyle="bold"
        android:shadowColor="@color/black"
        android:shadowRadius="1"
        android:shadowDx="1"
        android:shadowDy="1"
    />
    <TextView
        android:id="@+id/txtPosition"
        android:background="@color/blue_gray_semi"
        android:layout_alignParentBottom="true"
        android:layout_width="fill_parent"
        android:layout_height="48dp"
        android:gravity="center"
        android:padding="0dp"
        android:textColor="@color/white"
        android:textSize="24sp"
        android:textStyle="bold"
    />
    <ListView
        android:id="@+id/lvwAnswers"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/txtQuestion"
        android:layout_above="@id/txtPosition"
        android:listSelector="@drawable/list_item_colors"
        android:background="@drawable/list_border"
        style="@style/ListViews"
    />
</RelativeLayout>

Note that I defined some colors/styles/drawables...
You can live without, just to understand the concept.
Then, customize it at your will.

Now, you'll notice that The textView are defined BEFORE the ListView.
This is because so the ListView can refer to them in its "above" and "below" attributes.
If you were to use a LinearLayout, instead, you should define them top to bottom as they have to be displayed (Header, ListView and Footer)