Expandable List View code snippet for Android Studio
Welcome. Here in this post, you will learn about the expandable list view in Android Studio.
Expandable List View code Example for Android Studio
An expandable list view is a type of vertical list view that displays in a two level vertically scrolling list. It is however different from a list view because it has two levels that are groups which has its individual child items that can be either collapsed or expanded.
Adding an expandable list view in your App
- First Create a new project in Android Studio
- File ⇒ New Android ⇒ Application Project
- Then Open src -> package -> MainActivity.java and then add following code :
JAVA (MainActivity.java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import java.util.ArrayList; import java.util.HashMap; import java.util.List; public class ExpandableListView extends AppCompatActivity { private android.widget.ExpandableListView listView; private ExpandableListAdapter listAdapter; private List<String> listDataHeader; private HashMap<String,List<String>> listHash; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_expandable_list_view); listView = (android.widget.ExpandableListView)findViewById(R.id.expandableList); initData(); listAdapter = new ExpandableListAdapter(this,listDataHeader,listHash); listView.setAdapter(listAdapter); } private void initData() { listDataHeader = new ArrayList<>(); listHash = new HashMap<>(); listDataHeader.add("Months"); listDataHeader.add("Days"); List<String> Months = new ArrayList<>(); Months.add("January"); Months.add("February"); Months.add("March"); Months.add("April"); Months.add("May"); Months.add("June"); Months.add("July"); Months.add("August"); Months.add("September"); Months.add("October"); Months.add("November"); Months.add("December"); List<String> Days = new ArrayList<>(); Days.add("Sunday"); Days.add("Monday"); Days.add("Tuesday"); Days.add("Wednesday"); Days.add("Thursday"); Days.add("Friday"); Days.add("Saturday"); listHash.put(listDataHeader.get(0),Months); listHash.put(listDataHeader.get(1),Days); } } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".ExpandableListView"> <ExpandableListView android:id="@+id/expandableList" android:layout_width="match_parent" android:layout_height="match_parent"> </ExpandableListView> </android.support.constraint.ConstraintLayout> |
- Now Open res -> layout -> activity_main.xml and then add following code :
XML (activity_main.xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 |
JavaAdapterJavaXml import android.content.Context; import android.graphics.Typeface; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseExpandableListAdapter; import android.widget.TextView; import java.util.HashMap; import java.util.List; public class ExpandableListAdapter extends BaseExpandableListAdapter { private Context context; private List<String> listDataHeader; private HashMap<String,List<String>> listHashMap; public ExpandableListAdapter(Context context, List<String> listDataHeader, HashMap<String, List<String>> listHashMap) { this.context = context; this.listDataHeader = listDataHeader; this.listHashMap = listHashMap; } @Override public int getGroupCount() { return listDataHeader.size(); } @Override public int getChildrenCount(int i) { return listHashMap.get(listDataHeader.get(i)).size(); } @Override public Object getGroup(int i) { return listDataHeader.get(i); } @Override public Object getChild(int i, int i1) { return listHashMap.get(listDataHeader.get(i)).get(i1); } @Override public long getGroupId(int i) { return i; } @Override public long getChildId(int i, int i1) { return i1; } @Override public boolean hasStableIds() { return false; } @Override public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) { String headerTitle = (String)getGroup(i); if(view == null) { LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.expandablelistview_group,null); } android.widget.TextView lblListHeader = (android.widget.TextView)view.findViewById(R.id.lblListHeader); lblListHeader.setTypeface(null, Typeface.BOLD); lblListHeader.setText(headerTitle); return view; } @Override public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) { final String childText = (String)getChild(i,i1); if(view == null) { LayoutInflater inflater = (LayoutInflater)this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); view = inflater.inflate(R.layout.expandablelistview_item,null); } android.widget.TextView txtListChild = (TextView)view.findViewById(R.id.lblListItem); txtListChild.setText(childText); return view; } @Override public boolean isChildSelectable(int i, int i1) { return true; } } |
Output:
Finally, run this project.
This is how Expandable List View code snippet for Android Studio