Skip to main content

Android ExpandableListView Example

https://www.dev2qa.com/android-expandablelistview-example/

android.widget.ExpandableListView is a subclass of ListView. It divides the list items into several groups, each group contains multiple list items. To display data in it, you need to create an ExpandableListAdapter, and then override it’s several methods to provide group view item an child view item.

1. Steps To Use ExpandableListView.

  1. Create a layout xml file, add ExpandableListView xml element in it.
  2. Create an ExpandableListAdapter instance in activity java file.
  3. Get the ExpandableListView object in activity java file.
  4. Invoke ExpandableListView  setAdapter() method to set the data adapter for it.

2. ExpandableListAdapter Methods.

  1. getGroupCount() : Return the group item count.
  2. getChildrenCount(int groupIndex) : Return child item count in specified group by group index.
  3. getGroup(int groupIndex) : Return group object, commonly a String.
  4. getChild(int groupIndex, int childIndex) : Return specified child object by child index in specified group by group index.
  5. getGroupView(int groupIndex, boolean isExpanded, View view, ViewGroup viewGroup) : Return a View object that will be displayed for specified group item by groupIndex.
  6. getChildView(int groupIndex, int childIndex, boolean isLastChild, View view, ViewGroup viewGroup) : Return a View object that will be displayed for specified group child item.

3. ExpandableListView Example.

android expandable list view example
activity_expandable_list_view.xml
<ExpandableListView
    android:id="@+id/expandableListView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />
ExpandableListViewActivity.java
package com.dev2qa.example;

import android.database.DataSetObserver;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpandableListViewActivity extends AppCompatActivity {

    // Store group data.
    private List<String> groupList = null;
    // Store child data. Key is the group value, Value is the child data in a list.
    private Map<String, List<String>> childListMap = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_expandable_list_view);

        setTitle("dev2qa.com --- Expandable List View Example.");

        this.addUserInfo("Jerry", 35, "Engineer", "jerry@dev2qa.com");
        this.addUserInfo("Richard", 25, "Engineer", "richard@hotmail.com");
        this.addUserInfo("Tom", 35, "Engineer", "tom@dev2qa.com");
        this.addUserInfo("Jack", 25, "Tester", "jack@dev2qa.com");
        this.addUserInfo("Kevin", 35, "Tester", "kevin@dev2qa.com");

        // Create an ExpandableListAdapter object, this object will be used to provide data to ExpandableListView.
        ExpandableListAdapter expandableListAdapter = new ExpandableListAdapter() {
            @Override
            public void registerDataSetObserver(DataSetObserver dataSetObserver) {

            }

            @Override
            public void unregisterDataSetObserver(DataSetObserver dataSetObserver) {

            }

            @Override
            public int getGroupCount() {
                return groupList.size();
            }

            @Override
            public int getChildrenCount(int groupIndex) {
                String group = groupList.get(groupIndex);
                List<String> childInfoList = childListMap.get(group);
                return childInfoList.size();
            }

            @Override
            public Object getGroup(int groupIndex) {
                return groupList.get(groupIndex);
            }

            @Override
            public Object getChild(int groupIndex, int childIndex) {
                String group = groupList.get(groupIndex);
                List<String> childInfoList = childListMap.get(group);
                return childInfoList.get(childIndex);
            }

            @Override
            public long getGroupId(int groupIndex) {
                return groupIndex;
            }

            @Override
            public long getChildId(int groupIndex, int childIndex) {
                return childIndex;
            }

            @Override
            public boolean hasStableIds() {
                return true;
            }

            // This method will return a View object displayed in group list item.
            @Override
            public View getGroupView(int groupIndex, boolean isExpanded, View view, ViewGroup viewGroup) {
                // Create the group view object.
                LinearLayout groupLayoutView = new LinearLayout(ExpandableListViewActivity.this);
                groupLayoutView.setOrientation(LinearLayout.HORIZONTAL);

                // Create and add an imageview in returned group view.
                ImageView groupImageView = new ImageView(ExpandableListViewActivity.this);
                if(isExpanded) {
                    groupImageView.setImageResource(R.mipmap.ic_launcher_round);
                }else
                {
                    groupImageView.setImageResource(R.mipmap.ic_launcher);
                }
                groupLayoutView.addView(groupImageView);

                // Create and add a textview in returned group view.
                String groupText = groupList.get(groupIndex);
                TextView groupTextView = new TextView(ExpandableListViewActivity.this);
                groupTextView.setText(groupText);
                groupTextView.setTextSize(30);
                groupLayoutView.addView(groupTextView);

                return groupLayoutView;
            }

            // This method will return a View object displayed in child list item.
            @Override
            public View getChildView(int groupIndex, int childIndex, boolean isLastChild, View view, ViewGroup viewGroup) {
                // First get child text/
                Object childObj = this.getChild(groupIndex, childIndex);
                String childText = (String)childObj;

                // Create a TextView to display child text.
                TextView childTextView = new TextView(ExpandableListViewActivity.this);
                childTextView.setText(childText);
                childTextView.setTextSize(20);
                childTextView.setBackgroundColor(Color.GREEN);

                // Get group image width.
                Drawable groupImage = getDrawable(R.mipmap.ic_launcher);
                int groupImageWidth = groupImage.getIntrinsicWidth();

                // Set child textview offset left. Then it will align to the right of the group image.
                childTextView.setPadding(groupImageWidth,0,0,0);

                return childTextView;
            }

            @Override
            public boolean isChildSelectable(int groupIndex, int childIndex) {
                return false;
            }

            @Override
            public boolean areAllItemsEnabled() {
                return false;
            }

            @Override
            public boolean isEmpty() {
                return false;
            }

            @Override
            public void onGroupExpanded(int groupIndex) {

            }

            @Override
            public void onGroupCollapsed(int groupIndex) {


            }

            @Override
            public long getCombinedChildId(long groupIndex, long childIndex) {
                return 0;
            }

            @Override
            public long getCombinedGroupId(long groupIndex) {
                return 0;
            }
        };

        final ExpandableListView expandableListView = (ExpandableListView)findViewById(R.id.expandableListView);
        expandableListView.setAdapter(expandableListAdapter);

        // Add on group expand listener.
        expandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
            @Override
            public void onGroupExpand(int groupIndex) {
                // Get total group size.
                int groupListSize = groupList.size();

                // Close other expanded group.
                for(int i=0;i < groupListSize; i++) {
                    if(i!=groupIndex) {
                        expandableListView.collapseGroup(i);
                    }
                }
            }
        });
    }

    // Add user info in group and child object.
    private void addUserInfo(String name, int age, String title, String email)
    {
        if(this.groupList == null)
        {
            this.groupList = new ArrayList<String>();
        }

        if(this.childListMap == null)
        {
            this.childListMap = new HashMap<String, List<String>>();
        }

        if(!this.groupList.contains(name)) {
            this.groupList.add(name);
        }

        // Create child list.
        List<String> childList = new ArrayList<String>();
        childList.add("Title : " + title);
        childList.add("Age : " + String.valueOf(age));
        childList.add("Email : " + email);
        // Add child data list in the map, key is group name.
        this.childListMap.put(name, childList);
    }
}

Comments

Popular posts from this blog

web2apk

http://web2apk.com/create.aspx Create App   Intro   About   Changes   MalWare ?   Contact   Privacy Useful Links Bluetooth Mini Keyboards Android Mini PC Reset Android URL App Title Icon or

Android Bar Chart Using MpAndroidChart Library Tutorial

https://www.numetriclabz.com/android-bar-chart-using-mpandroidchart-library-tutorial/ Android Bar Chart Using MpAndroidChart Library Tutorial Objective In this tutorial we learn how to implement Bar Chart using MpAndroidChart Library in your Android App. Download Source Code       Step 1 Contents ·        1  Introduction ·        2  Creating Bar chart o    2.1  Create a new Project o    2.2  Adding library in Project o    2.3  Create Layout o    2.4  To Plot Bar Chart §   2.4.1  Initialize the graph id §   2.4.2  Creating a Dataset §   2.4.3  Defining X-axis labels §   2.4.4  Set the data §   2.4.5  Add the description to the chart §   2.4.6  Run your App § ...

how to retrieve image from sqlite database in android and display in listview

 Android platform provides several ways to store data in our application. 1. SQLite database 2. SharedPreferences etc For our post, we will only work with SQLite database. First and foremost, we need to understand what an SQLite database is? SQLite database  is an open source SQL database that stores data to a text file on a device. It executes SQL Commands to perform a set of functions, that is, create, read, update and delete operations. On my previous post, I showed how to  store data in SQLite database from edit text, retrieve and populate it in a listview . For this post, I will show the SQLite CRUD operations with images from gallery and text from EditText. We need to understand this; images are stored in SQLite database as BLOB data type. A BLOB is a large binary object that can hold a variable amount of data.  Note, we can only store images in the database as BLOB data type. We need to convert our image path to a bitmap th...