Sie sind auf Seite 1von 2

9/24/2017 android - How to add a Footer View (TextView) to the end of a ListView?

- Stack Overflow

Learn, Share, Build


Each month, over 50 million developers come to Stack Overflow to Google Facebook
learn, share their knowledge, and build their careers. OR

Join the worlds largest developer community.

How to add a Footer View (TextView) to the end of a ListView?

i am trying to make a layout like this:

actionbar
====================
txtViewTitle
--------------------
listview item1
listview item2
listview item3
........scroll scroll scroll
...at the end of all the items:
-----------------
txtViewFooter
==================
linearlayout of buttons [always visible at the bottom]
-------------------------------

the part between the ==========s should be scrollable

here's my code, it works except for txtViewFooter which is displayed if the list is short enough to accomodate in part of the screen. but not
displayed if list is longer and requores scroling

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/d_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<TextView
android:id="@+id/txtViewTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:background="#eeeeee"
android:gravity="center|center"
android:layout_alignParentTop="true"
style="?android:listSeparatorTextViewStyle"/>

<View
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_below="@+id/txtViewTitle"
android:id="@+id/separator"
android:visibility="visible"
android:background="@android:color/darker_gray"/>

<ListView
android:layout_height="match_parent"
android:layout_below="@+id/separator"
android:id="@+id/my_list"
android:listSelector="@android:color/transparent"
android:smoothScrollbar="true"
android:layout_width="fill_parent"
android:background="@android:color/transparent"
android:textColor="#000000"
android:dividerHeight="0dp"
android:cacheColorHint="@android:color/transparent">
</ListView>

<TextView
android:id="@+id/txtViewFooter"
android:layout_below="@+id/my_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="8dp"
android:textStyle="italic"
/>

<View

https://stackoverflow.com/questions/22388247/how-to-add-a-footer-view-textview-to-the-end-of-a-listview 1/2
9/24/2017 android - How to add a Footer View (TextView) to the end of a ListView? - Stack Overflow
android:layout_width="fill_parent"
android:layout_height="0.1dp"
android:layout_above="@+id/bottom_menu"
android:id="@+id/separator2"
android:visibility="visible"
android:background="@android:color/darker_gray"/>

<LinearLayout android:id="@+id/bottom_menu"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:background="#eeeeee">
<include layout="@layout/footer_menu" />
</LinearLayout>

android android-layout android-listview

edited Mar 13 '14 at 18:59 asked Mar 13 '14 at 18:53


Philipp Jahoda user1659274
31.1k 14 95 123

1 You can't use the footer in the way you want to. A footer is built into the list view, like an extra cell, and not a
bracket at the end. You need to just add a separate view - like a text view - and then set its content
programatically instead to using a footer. Rarw Mar 13 '14 at 18:56

1 Answer

Use

ListView.addFooterView(View v)

to add a footer to your ListView . You do not need to define the footer in .xml

Example:

TextView tv = new TextView(Context);


tv.setText("I am a footer")

ListView.addFooterView(tv);

Inflate from .xml:

View footer = LayoutInflater.from(Context).inflate(R.layout.your_footer_layout, null);


ListView.addFooterView(footer);

Make sure you add your footer or header View before you add other items to the list.

answered Mar 13 '14 at 18:55


Philipp Jahoda
31.1k 14 95 123

1 Just adding to your post, headers and footers are a fantastic thing to look into when it comes to UI
development for Android, it can make a world of difference in developing apps, especially with multiple
views. zgc7009 Mar 13 '14 at 19:00

that did it! thank you so much! ^_^ i didnt know about this listview footer thing! user1659274 Mar 13 '14 at
19:21

wait. a part of the footerview hides beneath the sticky bottom menu. how to solve this? user1659274 Mar
13 '14 at 19:23

probably an issue with another layout, have a look at your xml Philipp Jahoda Mar 13 '14 at 20:34

https://stackoverflow.com/questions/22388247/how-to-add-a-footer-view-textview-to-the-end-of-a-listview 2/2

Das könnte Ihnen auch gefallen