title: Add / Remove header and footer dynamically
body: <p>I need to be able to add and remove headers and footers from my <code>ListView</code> dynamically.   </p>
  
  <p>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 <code>Adapter</code>.</p>
  
  <p>So I found this solution, but it's ugly and I really hope that there is an other way.<br>
  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.</p>
  
  <p>Edit: I must add that using the visibility attribute (GONE &amp; VISIBLE) is not a solution here, because the headers &amp; footers views must not be in the adapter during my intermediate procedure.</p>
  
  <pre><code>    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&lt;String&gt;(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 &amp; 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&lt;String&gt;(TestAdapterHeader.this, -1));
                  getListView().addFooterView(mFooterView);
  
                  // Re set our Adapter
                  setListAdapter(mAdapter);
              }
  
              mParentView.requestLayout();
  
          }
      }
  </code></pre>