Home » Android » android – Add / Remove header and footer dynamically

android – Add / Remove header and footer dynamically

Posted by: admin June 15, 2020 Leave a comment

Questions:

I need to be able to add and remove headers and footers from my ListView dynamically.

So I initialize my activity with my headers and footers, then at some point I want to hide them, and later I need to add the previous headers and footers, and keep the same Adapter.

So I found this solution, but it’s ugly and I really hope that there is an other way.
Basically, I have to set a null adapter to be able to add the header view, and then set an empty adapter to add the footer view. To finish I set my real adapter.

Edit: I must add that using the visibility attribute (GONE & VISIBLE) is not a solution here, because the headers & footers views must not be in the adapter during my intermediate procedure.

    public class TestAdapterHeader extends ListActivity implements OnClickListener {
        private static String[] items = { "test 1", "test 2", "test 3", "test 4",
                "test 5", "test 6", "test 7", "test 8", "test 9", "test 10",
                "test 11", "test 12", "test 13", "test 14", "test 15", "test 16",
                "test 17", "test 18", "test 19", "test 20" };

        private ArrayAdapter mAdapter;
        private LinearLayout mParentView;
        private TextView mHeaderView, mFooterView;

        private boolean mViewsHidden = false;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            initViews();

            mAdapter = new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, items);
            setListAdapter(mAdapter);
        }


        private void initViews() {
            // The main layout
            mParentView = new LinearLayout(this);
            mParentView.setOrientation(LinearLayout.VERTICAL);
            mParentView.setBackgroundColor(Color.BLACK);

            // The button to hide the views
            Button hideViewsButton = new Button(this);
            hideViewsButton.setText("Add/Remove views");
            hideViewsButton.setOnClickListener(this);

            // The listview
            ListView listView = new ListView(this);
            listView.setId(android.R.id.list);
            listView.setCacheColorHint(Color.TRANSPARENT);

            mParentView.addView(hideViewsButton);
            mParentView.addView(listView);

            // Set the content view
            setContentView(mParentView);

            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT, 150);

            mHeaderView = new TextView(this);
            mHeaderView.setTextColor(Color.WHITE);
            mHeaderView.setBackgroundColor(Color.BLUE);
            mHeaderView.setGravity(Gravity.CENTER);
            mHeaderView.setLayoutParams(lp);
            mHeaderView.setText("Header");

            mFooterView = new TextView(this);
            mFooterView.setTextColor(Color.WHITE);
            mFooterView.setBackgroundColor(Color.BLUE);
            mFooterView.setGravity(Gravity.CENTER);
            mFooterView.setLayoutParams(lp);
            mFooterView.setText("Footer");


            getListView().addHeaderView(mHeaderView);
            getListView().addFooterView(mFooterView);
        }


        @Override
        public void onClick(View v) {
            mViewsHidden = !mViewsHidden;

            // Remove header & footer views
            if (mViewsHidden) {
                getListView().removeHeaderView(mHeaderView);
                getListView().removeFooterView(mFooterView);
            } 
            else {
                // Remove the ListAdapter to be able to add our headerView
                setListAdapter(null);
                getListView().addHeaderView(mHeaderView);

                // Set an empty ListAdapter to be able to add our footerView
                setListAdapter(new ArrayAdapter<String>(TestAdapterHeader.this, -1));
                getListView().addFooterView(mFooterView);

                // Re set our Adapter
                setListAdapter(mAdapter);
            }

            mParentView.requestLayout();

        }
    }
How to&Answers:

Do not try to add/remove the header/footer view.

To be able to add/remove header and footer view dynamically, simply add before setting the adapter a RelativeLayout to header and footer.
After this you can add/remove what ever you want to the RelativeLayouts.

Set the size of the RelativeLayouts to WRAP_CONTENT.

Harry

Answer:

You can use below code to do the same

// to show the footer view
footerView.setVisibility(View.VISIBLE); 

// to hide the footer view    
footerView.setVisibility(View.GONE); 

Answer:

I just had this problem and heres what i did. Add a tag to the view and find and remove it by the tag.

        final Button btnAddMore = new Button(this);
        btnAddMore.setTag("footer");   

        if(myList.getFooterViewsCount() >0)
        {
            View v = myList.findViewWithTag("footer");
            if(v != null)
            {
                 myList.removeFooterView(v);
            }           
        }

Answer:

You should use view.setVisibility(int visibility) View.GONE,View.VISIBLE or View.INVISIBLE

        ....
        if (mViewsHidden) {
            mHeaderView.setVisibility(View.GONE);
            mFooterView.setVisibility(View.GONE);
        } 
        .... 
        else {
            mHeaderView.setVisibility(View.VISIBLE);
            mFooterView.setVisibility(View.VISIBLE);
        } 
        .... 

Answer:

you need add footers and headers views in listview before the setAdapter and after this you can manipulate with setVisibility

good luck 😉

Answer:

This seems to be one of the few solutions out there. I just tried something very similar and it worked, but I’m in the same boat. To me, it feels like a hack.

There’s a few other options, like manually adding/removing/hiding the header/footer into a view outside the listview. However, you may not get the nice scrolling effect that’s pretty much the point of a listview header.

I just tried setting a view to View.GONE and instead of the view vanishing, there’s a gap at the top of the ListView.

Answer:

I found a work around to this situation, though most may not like it. Just add a linear layout above and below the list view. Now you can dynamically add and remove views with the option of using visibility options. If you set the parameters to WRAP_CONTENT, then the layout will take up no space if there are no children.

Answer:

I got similar problem- I had to dynamically add or remove header and I did it in this way:
– I removed items from adapter and set adapter=null;
– For removing: removeHeaderView(mHeaderView);
– For adding header: addHeaderView(mHeaderView);
– create adapter again and set content

It works, but without animation it looks ugly

Answer:

its work fine for me!  

 private void addHeaderList(){
       mListView.setAdapter(null);
       mListView.addHeaderView(headerList);

   }
   private  void removeHeaderList(){
       mListView.removeHeaderView(headerList);
   }

     just called call function before setadapter again.
            addHeaderList();
            mListView.setAdapter(mAdapter);

Answer:

Its very simple!

1) Provide and id to the layout container of you footer.

Ex. footer.xml

 <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:id="@+id/parent_footer_search_list"
        android:layout_height="fill_parent"
        android:background="#fff"
        android:orientation="vertical">
       <TextView

            android:layout_width="fill_parent"
            android:layout_height="fill_parent"

            android:gravity="center|left"
            android:lineSpacingExtra="5dp"

            android:text="Create new list"


            android:textColor="#666666"
            android:textSize="16.8sp"
            android:textStyle="bold" />
      </LinearLayout>

In the above xml, parent_footer_search_list is your main container id.
Now let’s come back to your fragment/activity where you’ve inflated this footer.
So, in order to remove it,

LinearLayout parentContainerForFooter = (LinearLayout) footer.findViewById(R.id.parent_footer_search_list);

**yourview**.removeFooterView(parentContainerForFooter); 

Done! This way, you are using correct android standards. Don’t using wrap_content, visibility GONE, etc. They are just hacks !

Answer:

Try using list.addFooterView(footerview) for footer and list.addHeaderView(headerview) for header.