Development issue/problem:

I know there is an ExpandableListView, but it only supports two levels. I need a true vertical list to view trees on at least ~5 levels (more is better).

Do you have any suggestions?


I see a conversation about using a custom adapter and adjusting the fill to the level of the element.

I have an unsorted ArrayList of objects that have an ID and a parent ID, and I am dynamically adding items to this array.

Can someone give me some examples of how to do that?

How can I solve this problem?

Solution 1:

I had the same problem. You can check out my implementation of AndroidTreeView.

  • His tree is level N.
  • Individual style for nodes
  • Saving the status after rotation

Solution 2:

Our company also offers an open source solution for this. It is available as a library and therefore very easy to use:

Enter a description of the image here

Solution 3:

I solved it myself by publishing in a similar thread:
another thread.

Enter a description of the image here

Solution 4:

To answer my own question, since we implemented this measure many months ago.

Our implementation in an open source project.

Solution no. 5:

I’ve found a simpler solution to this problem, having intervened a bit myself in my programming skills. In my situation, I needed a tree structure similar to Windows, which I was able to set up after some thought and without any programming!

Here’s the catch: Use WebView and an embedded HTML page to display a custom tree structure and use Android’s convenient JavaScript communication interface to get selections and clicks: Example of a proof of concept on the Android-er blog

With this power, we can take advantage of a huge collection of JS/CSS control snippets on the web. Managing the jQuery Windows7 style tree – jBoom

Many features and performance with Android, happy coding!

Solution no. 6:

I found the link below very, very helpful. It describes the various ways in which tree structures can be stored in two-dimensional data structures (usually a database table).

I think the paradigm is easy to understand and implement.

Managing Hierarchical Data in MySQL

How to visualize this in Android is another question. I would probably write my own widget from scratch if the list of articles solution didn’t work.

Solution no. 7:

I think if the layered list is done right, it really works and looks great.
Here is another example

Enter a description of the image here

Solution no. 8:

I agree with PJV, at least for telephones. It would be better to organize a widget to show a group of siblings at once in the ListView. This can be done in a single action that tracks their position in the tree. It can display a breadcrumb header that shows the path to the parent of the currently displayed items.

A multi-level tree structure may work for a tablet, but a phone doesn’t have enough space to support the suggested 5 levels (everything is big enough for your fingers).

However, if you are configured for a tree view, you should not bother with the ExpandableListView subclass. It works internally by grouping the parent and child indices (each an int) into a long. This internal representation makes it almost impossible to cross the 2 levels.

Solution no. 9: ;

import;import android.os.bundle;import android.view.ContextMenu;import android.view.Gravity;import android.view.MenuItem;import android.view.view;import android.view.viewGroup;import android.view.ContextMenuInfo;import android.view.widget.AbsListView;import android.widget.BaseExpandableListAdapter;import android.widget.ExpandableListAdapter;import android.widget.ExpandableListView;import android.widget.TextView;import android.widget.Toast;import android.widget.ExpandableListView.ExpandableListContextMenuInfo ;

/** Displays expandable lists using the custom {@BaseExpandableListAdapter}
public class extends the expandable list {

Expandable ListAdapter mAdapter ;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState) ;

// Configure our adapter
mAdapter = new MyExpandableListAdapter();

public void onCreateContextMenu(menuMenuContext, View v, ContextMenuInfo menuInfo) {
menu.add(0, 0, 0, R.string.expandable_list_sample_action);

@Check public logical item
onContextItemSelected(MenuItem item) {
ExpandableListContextMenuInfo = (ExpandableListContextMenuInfo) item.getMenuInfo() ;

Title string = ((TextView) info.targetView).getText().toString() ;

int type = ExpandableListView.getPackedPositionType(info.packedPosition);
if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
int childPos = ExpandableListView.getPackedPositionChild(info.packedPosition);
Toast.makeText(this, title +: Child + childPos + clicked into group + groupPos,
return true;
} else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
int groupPos = ExpandableListView.getPackedPositionGroup(info.packedPosition);
Toast.makeText(this, title +: groupPos + clicked, Toast.LENGTH_SHORT).show();
returns true;

Reverse false;

/** A simple adapter that supports an ArrayList photo source display.
* Each picture is displayed as a single picture. This adapter allows you to remove the
* picture frame and add a new picture.
public class MyExpandableListAdapter extends the BaseExpandableListAdapter {
// Example record. children[i] contains child elements (String[]) for groups[i].
private String[] groups = { Category1, Category2, Category3, Category4 } ;
private String[][] children = {
{ Charity1, Charity2, Charity3, Charity4 },
{ Charity5, Charity6, Charity7, Charity8 },
{ Charity9, Charity10 },
{ Charity11, Charity12 }
} ;

public Object getChild(int groupPosition, int childPosition) {
return child objects [groupPosition] [childPosition] ;

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

public int getChildrenCount(int groupPosition) [
returns the child elements [groupPosition].length ;

public TextView getGenericView() {
// Layout parameters for ExpandableListView
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64) ;

TextView textView = new TextView(expandands.this);
// vertical text in the middle
textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
// set start position of text
textView.setPadding(36, 0, 0, 0);
return textView;

public view getChildView(int groupPosition, int childPosition, boolean isLastChild,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition, int childPosition).toString());
returns textView;

public object getGroup(int groupPosition) [
returns the group [groupPosition];

public int getGroupCount() {
returns group.length;

public lang getGroupId(int groupPosition) {
returns groupPosition;

public view getGroupView(int groupPosition, boolean isExpanded, View convertView,
ViewGroup parent) {
TextView textView = getGenericView();
returns textView;

public boolean isChildSelectable(int groupPosition, int childPosition) {
returns true;

public boolean hasStableIds() {
returns true;


Solution no. 10:

For starters, I would like to make the data structure more representative of what it should contain. Since you have a set of elements, and each child can have their own set of elements, each with their own set, etc., you can have a set of elements. I would consider using a class with two members: an object that represents the data for that particular element, and a table that contains the element’s children. Each child will be an example of the class itself, so they can have children too.

ParentAndChildren private class {
Parent object;
Array children;

Your adapter has a set of ParentAndKids items that are part of the top tier. You add and remove items from the list based on the parents who have expanded.

Good luck!

 unity tree view in gameunity treeviewunity ui treeviewunity treeview context menuunity repair toolunity editor panelsandroid family treebest family tree software ukwe're related app alternativehow accurate is family tree apphelp with ancestry researchbest family tree website ukandroid studio tutorial pdf 2020android studio book 2019android lecture notes pdfandroid studio tutorial pdf in hindiandroid tutorial for beginners w3schools pdfmobile application development tutorial pdfandroid treeview exampleeasy tree view for androidandroid treeview libraryunity treeview exampletreeview team-blox androiddynamic tree view androidtree structure ui in androidandroid treeview recyclerview