Android ExpandableListView Add Footer Dynamically

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="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/footer_layout">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="FOOTER"/>
</LinearLayout>

3. Now create the “MyActivity” class:

import android.app.Activity;
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;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mExpandableList = (ExpandableListView) findViewById(R.id.expandable_list);

        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);
            }
            parent.setArrayChildren(arrayChildren);

            //in this array we add the Parent object. We will use the arrayParents at the setAdapter
            arrayParents.add(parent);
        }

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

        footerLayout = (LinearLayout) view.findViewById(R.id.footer_layout);

        // Add the footer before the setAdapter() method
        mExpandableList.addFooterView(footerLayout);

        //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.
        mExpandableList.removeFooterView(footerLayout);

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

    }

    /**
     * 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() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                // 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() {
                    @Override
                    public void run() {
                        Toast.makeText(MyActivity.this, "Footer displayed!", Toast.LENGTH_SHORT).show();
                        mExpandableList.addFooterView(footerLayout);
                    }
                });

            }
        }).start();
    }
}

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 🙂

, , ,
Previous Post
Android TCP Connection Enhanced
Next Post
Android: ListView with SearchView

Related Posts

Menu