home top ad

ExpandableListView in android studio

Hello friend today we are learn how to create expandableListView in android studio.
 activity_main.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    android:background="#f4f4f4" >
    <ExpandableListView
        android:id="@+id/expandable_listview"
        android:layout_height="match_parent"
        android:layout_width="match_parent"  />
</LinearLayout>
 MainActivity.java 
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class ExpandedActivity extends AppCompatActivity {
    ExpandableListView expandableListView;
    MyExpandableListViewAdapter myExpandableListViewAdapter;
    List<String> category;
    Map<String, List<String>> names;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.expanded_activity);
        expandableListView = findViewById(R.id.expandable_listview);
        getAllData();
        myExpandableListViewAdapter = new MyExpandableListViewAdapter(this, category, names);
        expandableListView.setAdapter(myExpandableListViewAdapter);
        expandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {  // TODO Auto-generated method stub 41
                Toast.makeText(getApplicationContext(), category.get(groupPosition) + " : " + names.get(category.get(groupPosition)).get(childPosition), Toast.LENGTH_SHORT).show();
                return false;
            }
        });
    }

    private void getAllData() {
        category = new ArrayList<String>();
        names = new HashMap<String, List<String>>();
        category.add("Fruits");
        category.add("Flowers");
        category.add("Animals");
        List<String> fruits = new ArrayList<String>();
        fruits.add("Mango");
        fruits.add("Orange");
        fruits.add("Banana");
        fruits.add("Apple");
        List<String> flowers = new ArrayList<String>();
        flowers.add("Rose");
        flowers.add("Lotus");
        flowers.add("Jasmine");
        flowers.add("Sunflower");
        List<String> animals = new ArrayList<String>();
        animals.add("Lion");
        animals.add("Tiger");
        animals.add("Elephant");
        animals.add("Bear");
        names.put(category.get(0), fruits);
        names.put(category.get(1), flowers);
        names.put(category.get(2), animals);
    }
  }


 MyExpandableListViewAdapter.java 
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;

import java.util.List;
import java.util.Map;
import java.util.Objects;

public class MyExpandableListViewAdapter extends BaseExpandableListAdapter {
    List<String> category;
    Map<String, List<String>> names;
    Context context;

    public MyExpandableListViewAdapter(Context context, List<String> category, Map<String, List<String>> names) {
        this.context = context;
        this.category = category;
        this.names = names;
    }

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

    @Override
    public int getChildrenCount(int groupPosition) {
        return Objects.requireNonNull(names.get(category.get(groupPosition))).size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return category.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return Objects.requireNonNull(names.get(category.get(groupPosition))).get(childPosition);
    }

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

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

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

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item2, null);
        }
        TextView txtParent = convertView.findViewById(R.id.parent_list);
        txtParent.setText(headerTitle);
        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        final String childText = (String) getChild(groupPosition, childPosition);
        if (convertView == null) {
            LayoutInflater infalInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = infalInflater.inflate(R.layout.item3, null);
        }
        TextView txtChild = convertView.findViewById(R.id.child_list);
        txtChild.setText(childText);
        return convertView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}
 item2.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/parent_list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Parent Text"
        android:layout_marginLeft="16dp"
        android:textAppearance="?android:attr/textAppearanceLarge"/> </LinearLayout>
 item3.xml 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:padding="16dp"
    android:orientation="vertical">
    <TextView
        android:id="@+id/child_list"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Child Text"
        android:layout_gravity="center"  />
</LinearLayout>
Below is the screenshot how this app will work.
Thank you

Post a Comment

0 Comments