Android ExpandableListView Add Footer Dynamically

Hi, recently I ran into a problem with footer view on an ExpandableListView. I wanted to add a footer view only when I would have needed it. I tried many ways to achieve this, but the only way that worked, in my case, was to add the footer and just after it to remove it, and only after this “trick” was done, I could add the footer anywhere in the code.

So I will show in this tutorial how I did this. It’s very simple 🙂

1. First you have to follow the steps from Android ExpandableListView Example, but when you will get to “MyActivity” class you will have to ignore it and follow the steps from this tutorial. So, you must create all the classes from that tutorial except “MyActivity” which will be created here.

2. Create a new xml file under res-layout, called “footer_view.xml”. The code is below:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""


3. Now create the “MyActivity” class:

import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.LinearLayout;
import android.widget.Toast;

import java.util.ArrayList;

public class MyActivity extends Activity {

    private ExpandableListView mExpandableList;
    private LinearLayout footerLayout;

    public void onCreate(Bundle savedInstanceState) {

        mExpandableList = (ExpandableListView) findViewById(;

        ArrayList<Parent> arrayParents = new ArrayList<Parent>();
        ArrayList<String> arrayChildren;

        //here we set the parents and the children
        for (int i = 0; i < 2; i++) {
            //for each "i" create a new Parent object to set the title and the children
            Parent parent = new Parent();
            parent.setTitle("Parent " + i);

            arrayChildren = new ArrayList<String>();
            for (int j = 0; j < 2; j++) {
                arrayChildren.add("Child " + j);

            //in this array we add the Parent object. We will use the arrayParents at the setAdapter

        View view = getLayoutInflater().inflate(R.layout.footer_view, mExpandableList, false);

        footerLayout = (LinearLayout) view.findViewById(;

        // Add the footer before the setAdapter() method

        //sets the adapter that provides data to the list.
        mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this, arrayParents));

        // Now that we are after the setAdapter() method we have to remove the added footer.

        // Now that this trick is done, we can use the addFooterView() whenever we want in the code.


     * Displays the footer on the screen after 3 seconds.
    private void displayFooterAfterDelay(){
        // We create a thread that will sleep for 3 seconds.
        new Thread(new Runnable() {
            public void run() {
                try {
                } catch (InterruptedException e) {

                // 3 seconds have passed so we have to make the adapter to be displayed on the screen.
                // The UI elements cannot be displayed on this thread so we have to make them to be displayed
                // on the MAIN THREAD. To do this we have to use the "runOnUiThread() method"
                runOnUiThread(new Runnable() {
                    public void run() {
                        Toast.makeText(MyActivity.this, "Footer displayed!", Toast.LENGTH_SHORT).show();


So that’s it. The footer will appear after 3 seconds (3000 ms) on the screen. You can also try to make the footer gone at first (before setAdapter) and in the thread to make it visible. If it works this way, I recommend this approach. But for some reason, in my case it didn’t work, so I had to use the approach described in this tutorial.

I hope that this tutorial will help anybody who ran into the same problem as me 🙂