Sunteți pe pagina 1din 3

9/24/2017 Android ListActivity - fixed header and footer - 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.

Android ListActivity - fixed header and footer

Is it possible to set header and footer at ListActivity to be fixed at the top and the bottom, so only the content (list) is scrolling, not also
header and footer?

I have both set like this:

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


View footer = getLayoutInflater().inflate(R.layout.list_footer, null);
ListView listView = getListView();
listView.addHeaderView(header);
listView.addFooterView(footer);

android header footer listactivity fixed

asked Nov 15 '12 at 16:21


Trick
2,017 8 37 71

3 Answers

You can achieve it by using a custom XML layout in which you will set the layout of your
header, footer and list.

Note that to be compatible with ListActivity this layout must contain a ListView with the id
android.R.id.list :

<?xml version="1.0" encoding="utf-8"?>


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HEADER" />

<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FOOTER" />

</LinearLayout>

And set it in your ListActivity like this:

public class TestActivity extends ListActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}

edited Mar 6 '16 at 17:21 answered Nov 15 '12 at 16:40

https://stackoverflow.com/questions/13401760/android-listactivity-fixed-header-and-footer 1/3
9/24/2017 Android ListActivity - fixed header and footer - Stack Overflow
fiddler
11.6k 7 52 106

According to project scenario, this methodology would be better than any other approach,
because if you want to customize the header and footer, you can make changes in appropriate
header and footer layout. In your layout xml file follow this :

In main.xml

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

<include
android:id="@+id/header_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/header.xml" />

<ListView
android:id="@+id/list_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:below="@id/header_layout"
android:above="@id/footer_layout" />

<include
android:id="@+id/footer_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="@layout/footer.xml" />
</RelativeLayout>

In header.xml

<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:id="@+id/header_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Your Application Title"/>
</LinearLayout>

In footer.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<Button
android:id="@+id/done_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Done"/>
</LinearLayout>

In Activity, Here

public class MainActivity extends ListActivity {

private ListView mListView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mListView = (ListView) findViewById(R.id.list_view);
// Now you can do whatever you want

}
}

edited Nov 15 '12 at 16:59 answered Nov 15 '12 at 16:49


RajeshVijayakumar
6,214 10 38 71

You can do it this way given below:

https://stackoverflow.com/questions/13401760/android-listactivity-fixed-header-and-footer 2/3
9/24/2017 Android ListActivity - fixed header and footer - Stack Overflow
//your adapter for listview
mAdapter = new ToDoListAdapter(getApplicationContext());

// Put divider between ToDoItems and FooterView


getListView().setFooterDividersEnabled(true);

// TODO - Inflate footerView for footer_view.xml file


LayoutInflater layoutInflater = getLayoutInflater();

//text view or whatever you have for your footer


TextView footerView = null;
footerView=(TextView)layoutInflater.inflate(R.layout.footer_view,null);

// TODO - Add footerView to ListView

getListView().addFooterView(footerView);
getListView().setAdapter(mAdapter);

This is inside of your onCreate() method. It worked for me. give comments if you find any
bugs here

answered Jan 29 '15 at 21:39


Omar Faroque Anik
1,318 13 21

https://stackoverflow.com/questions/13401760/android-listactivity-fixed-header-and-footer 3/3

S-ar putea să vă placă și