Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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"?><LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="HEADER" /><ListViewandroid:id="@android:id/list"android:layout_width="match_parent"android:layout_height="0dp"android:layout_weight="1" /><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="FOOTER" /></LinearLayout>

And set it in your ListActivity like this:

publicclassTestActivityextendsListActivity {

    @OverrideprotectedvoidonCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

Solution 2:

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"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent" ><includeandroid:id="@+id/header_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"layout="@layout/header.xml" /><ListViewandroid:id="@+id/list_view"android:layout_width="fill_parent"android:layout_height="wrap_content"android:below="@id/header_layout"android:above="@id/footer_layout" /><includeandroid:id="@+id/footer_layout"android:layout_width="fill_parent"android:layout_height="wrap_content"layout="@layout/footer.xml" /></RelativeLayout>

In header.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><TextViewandroid:id="@+id/header_text_view"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Your Application Title"/></LinearLayout>

In footer.xml

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="vertical" ><Buttonandroid:id="@+id/done_button"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Done"/></LinearLayout>

In Activity, Here

publicclassMainActivityextendsListActivity {

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

   }
 }

Solution 3:

You can do it this way given below:

//your adapter for listview
    mAdapter = newToDoListAdapter(getApplicationContext());

    // Put divider between ToDoItems and FooterView
    getListView().setFooterDividersEnabled(true);

    // TODO - Inflate footerView for footer_view.xml fileLayoutInflaterlayoutInflater= getLayoutInflater();

    //text view or whatever you have for your footerTextViewfooterView=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

Post a Comment for "Android Listactivity - Fixed Header And Footer"