Click here to Skip to main content
15,867,750 members
Articles / Mobile Apps / Android
Tip/Trick

Tree View in Java For Android

Rate me:
Please Sign up or sign in to vote.
4.89/5 (7 votes)
13 Sep 2016CPOL1 min read 24K   509   3   6
Tree View in Swift for Android that is easy to implement and use

Introduction

I love the TreeView in .NET and want one for an Android app, but discovered that Android does not have a TreeView control so after hunting around, I just decided to write one of my own.

Background

I was unable to find any TreeViews at all online, and the closest thing was a 2 level tree with a checkbox, but I did not ever look at it because I did not like it, so this is all my own work.

Using the Code

I did this in Android Studio and started with an empty project. TreeListActivity is the Main Activity and uses tree_view_cell in the Layout directory.

Even zipped, the project is too big to include in the article, so I Zipped the "main" section so you can get the source code and resources.

The data for the Tree is loaded in TreeViewLists.java. It is an ArrayList of a class called TreeViewData. Be sure to use a unique id for each one and then for the children nodes, set the parentId to the id of the node they belong under. Set the zero level parentId to something that is not going to be used.

Java
public static ArrayList<TreeViewData> LoadInitialData()
{
    ArrayList<TreeViewData> data = new ArrayList<TreeViewData>();

    data.add(new TreeViewData(0, "cindy's family tree", "1", "-1"));
    data.add(new TreeViewData(0, "jack's family tree", "2", "-1"));
    data.add(new TreeViewData(1, "katherine", "3",  "1"));
    data.add(new TreeViewData(1, "kyle", "4", "1"));
    data.add(new TreeViewData(2, "hayley","5", "3"));
    data.add(new TreeViewData(2, "macey", "6", "3"));
    data.add(new TreeViewData(1, "katelyn", "7", "2"));
    data.add(new TreeViewData(1, "jared", "8", "2"));
    data.add(new TreeViewData(1, "denyee", "9", "2"));
    data.add(new TreeViewData(2, "cayleb", "10", "4"));
    data.add(new TreeViewData(2, "carter", "11", "4"));
    data.add(new TreeViewData(2, "braylon", "12", "4"));
    data.add(new TreeViewData(3, "samson", "13", "5"));
    data.add(new TreeViewData(3, "samson", "14", "6"));

    return data;
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Systems Engineer
United States United States
I Graduated with a Computer Information Science Degree in 1990 and have been working developing systems since then.

I started in COBOL, moved to PROGRESS, did a little RPG, then moved on to Mobile apps and desktop applications.

It's been a great journey Smile | :)

Comments and Discussions

 
QuestionTree View Pin
Member 117569034-Jan-20 21:27
Member 117569034-Jan-20 21:27 
Questionsearch functionality into this Tree View? Pin
Member 130308971-Mar-17 23:52
Member 130308971-Mar-17 23:52 
GeneralMy vote of 5 Pin
Member 1275273522-Sep-16 5:26
Member 1275273522-Sep-16 5:26 
GeneralRe: My vote of 5 Pin
pgmr_6480422-Sep-16 5:49
pgmr_6480422-Sep-16 5:49 
Thanks. I am working on another little sample app to put up too that I have not seen any samples for that should help people.
QuestionSwift ? Pin
Serge Desmedt13-Sep-16 9:48
professionalSerge Desmedt13-Sep-16 9:48 
AnswerRe: Swift ? Pin
pgmr_6480413-Sep-16 15:25
pgmr_6480413-Sep-16 15:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.