Search code examples
androidandroid-listviewfooter

How to add a footer in ListView?


I am developing an application,In my application,I am using Listview for displaying data using dom parsing,I want to footer in listview,when i click footer additional more data add to list view,I attached image,i would like that design and process,please refer image1 and imgae2.I mention footer in red rectangle

Fig1-Footer like "More News"
alt text

alt text

Fig2-Add additional 10 record added in listview


Solution

  • Create a footer view layout consisting of text that you want to set as footer and then try

    View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
    ListView.addFooterView(footerView);
    

    Layout for footer could be something like this:

    <?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="wrap_content"
        android:paddingTop="7dip"
        android:paddingBottom="7dip"
        android:orientation="horizontal"
        android:gravity="center">
    
        <LinearLayout 
            android:id="@+id/footer_layout" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            android:gravity="center"
            android:layout_gravity="center">
    
        <TextView 
            android:text="@string/footer_text_1" 
            android:id="@+id/footer_1" 
            android:layout_width="wrap_content" 
            android:layout_height="wrap_content" 
            android:textSize="14dip" 
            android:textStyle="bold" 
            android:layout_marginRight="5dip" />
        </LinearLayout>
    </LinearLayout> 
    

    The activity class could be:

    public class MyListActivty extends ListActivity {
        private Context context = null;
        private ListView list = null;
    
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            list = (ListView)findViewById(android.R.id.list);
    
            //code to set adapter to populate list
            View footerView =  ((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
            list.addFooterView(footerView);
        }
    }