All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Dynamic Expandablelistview In Android Example

Last Updated : Mar 11, 2024

Dynamic Expandablelistview In Android Example

In this article we will show you the solution of dynamic expandablelistview in android example, a view called Android ExpandableListView displays items in a two-level list that scrolls vertically.

By allowing two layers, which are groupings that can be conveniently enlarged or collapsed via touching to see, it varies from a ListView.

Android's ExpandableListViewAdapter loads the data into the view's associated items. These are some significant techniques this class employs:

setChildIndicator(Drawable) displays an indication next to each object that depicts its current status.

The state state would be set if the child is the very last child in the group.

Set GroupIndicator(Drawable): A drawable indicator is displayed next to the group to show whether it is expanded or collapsed.

State empty would be set if the group was empty. The state state expanded would be set if the group has been expanded.

getGroupView(): It provides a list group header view. getChildView() : It provides the list child item's perspective.

The indicator for Android An item indicator's left bound is left. Note:

Unless the parent's size is explicitly given, you could use value wrap content for the ExpandableListView's android:layout_height attribute in XML.

The following describes the group header design for each separate list: list group.xml

Step By Step Guide On Dynamic Expandablelistview In Android Example :-

XML

<?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>

Java

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);
            arrayParents.add(parent);
        }
        View view = getLayoutInflater().inflate(R.layout.footer_view, mExpandableList, false);
        footerLayout = (LinearLayout) view.findViewById(R.id.footer_layout);
        mExpandableList.addFooterView(footerLayout);
        mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this, arrayParents));
              mExpandableList.removeFooterView(footerLayout);
               displayFooterAfterDelay();
    }
    /**
     * Displays the footer on the screen after 3 seconds.
     */
    private void displayFooterAfterDelay(){
               new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    Thread.sleep(3000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                // 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();
    }
}
  1. First you need to Open Android Studio and create a new project. The programming should be set to Java.
  2. then we create A ListView that is the root view of activity_main.xml. Specify the appropriate ID for the ListView in activity_main.xml by executing the following code.
  3. Then we set the parents and the children
  4. After in this array we add the Parent object. We will use the arrayParents at the setAdapter.
  5. Now that we are after the setAdapter() method we have to remove the added footer.
  6. Now that this trick is done, we can use the addFooterView() whenever we want in the code.
  7. Then We create a thread that will sleep for 3 seconds.
  8. Then 3 seconds have passed so we have to make the adapter to be displayed on the screen.
  9. The UI elements cannot be displayed on this thread so we have to make them to be displayed

Conclusion :-

The view known as Android ExpandableListView is used to display items as a two-level list that scrolls vertically.

The main distinction between ListView and other display options is that ListView supports 2 levels of the hierarchy: groups which can be conveniently expanded & collapsed whilst also touching to view, and their corresponding children's items.

Android uses ExpandableListViewAdapter to display the view.

ExpandableListView functionality is necessary in many applications.

I hope this article on dynamic expandablelistview in android example helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Ashish

Ashish is a dynamic and motivated individual with a passion of programming and an experienced programmer having 3+ years of experience in various languages like Java, Python, HTML, CSS, JavaScript, jQuery and various frameworks like Bootstrap

Follow Ashish On Linkedin 🡪