Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

TreeView in Android with Example

  • Last Updated : 18 Feb, 2021

If you are looking for the new UI designs for the representation of huge data, then there are so many ways for the representation of this type of data. You can use pie charts, graphs, and many more view types for the implementation of these views. For displaying such huge data then we can prefer using a TreeView. TreeView is similar to that of a tree in which it is having a parent node and inside that parent node, you can create multiple nodes according to requirement. In this example, we can take a look at creating a TreeView in your Android application. Now we will move towards the implementation of Tree View. Note that we are going to implement this project using the Java language. 

What is TreeView and How it looks? 

TreeView is a pattern for the representation of data in the form of the tree so that it becomes easier for users to understand the organization of data in our app. A sample image is given below to get an idea of how TreeView looks like.

TreeView in Android


Step 1: Create a New Project

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Java as the programming language.

Step 2: Add dependency to build.gradle(Module:app)

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.    

implementation ‘de.blox.treeview:treeview:0.1.0’

After adding this dependency sync your project and now we will move towards its implementation. 

Step 3: Modify the strings.xml file

Below is the code for the strings.xml file.


    <string name="app_name">GFG App</string>
    <string name="my_node">Node</string>

Step 4: Working with the activity_main.xml file

Navigate to the app > res > layout > activity_main.xml and add the below code to that file. Below is the code for the activity_main.xml file.   


<?xml version="1.0" encoding="utf-8"?>
    <!--Below is the code for tree view-->
        android:layout_height="match_parent" />

Step 5: Create a new XML file

After adding this TreeView create a new XML file for your node which we have to display inside our TreeView. For creating a new XML file navigate to the app > res > layout > Right-click > New > Layout Resource file. Give a name to your file (Here we have given tree_view_node) and click on create. After creating this file add the below code to it. Below is the code for the tree_view_node.xml file. 


<?xml version="1.0" encoding="utf-8"?>
        android:textStyle="bold" />

Step 6: Create a new Java class 

Now create a new Java class as View Holder for handling our nodes in Tree View. To create a new java class please refer to the How to Create Classes in Android Studio. After creating a new java class add the below code to it. Here we have named the class as Viewholder. Below is the code for the file. 


import android.view.View;
import android.widget.TextView;
public class Viewholder {
    TextView textView;
    Viewholder(View view) {
        textView = view.findViewById(;

After creating the Viewholder class then we will move towards the implementation of TreeView in our file. 

Step 7: Working with the file

Go to the file and refer to the following code. Below is the code for the file. Comments are added inside the code to understand the code in more detail.


import android.os.Bundle;
import android.view.View;
import androidx.annotation.NonNull;
import de.blox.treeview.BaseTreeAdapter;
import de.blox.treeview.TreeNode;
import de.blox.treeview.TreeView;
public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        // creating a variable for tree view.
        TreeView treeView = findViewById(;
        // creating adapter class for our treeview using basetree adapter.
        // inside base tree adapter you have to pass viewholder class along 
        // with context and your layout file for treeview node.
        BaseTreeAdapter<Viewholder> adapter = new BaseTreeAdapter<Viewholder>(this, R.layout.tree_view_node) {
            public Viewholder onCreateViewHolder(View view) {
                return new Viewholder(view);
            public void onBindViewHolder(Viewholder viewHolder, Object data, int position) {
                // inside our on bind view holder method we
                // are setting data from object to text view.
        // below line is setting adapter for our tree.
        // below tree node is a parent node of our tree 
        // node which is Geeks for Geeks.
        TreeNode root = new TreeNode("Geeks for Geeks");
        // below node is the first child node of our root node ie Geeks for Geeks.
        TreeNode DSAchildNode = new TreeNode("DSA");
        // below node is the second child of our 
        // root node ie Geeks for Geeks.
        TreeNode AlgoChildNode = new TreeNode("Algorithm");
        // below node is the third child of our
        // root node ie Geeks for Geeks.
        TreeNode languageNode = new TreeNode("Language");
        // below node is the first child of our language node.
        TreeNode CchildNode = new TreeNode("C++");
        // below node is the second child of our language node.
        TreeNode javaChildNode = new TreeNode("Java");
        // below node is the first child of our DSA node.
        TreeNode arrayChild = new TreeNode("Arrays");
        // below node is the second child of our DSA node.
        TreeNode stringChild = new TreeNode("Strings");
        // below node is the first child of our Algorithm node.
        TreeNode sortingChildNode = new TreeNode("Sorting");
        // below lines is used for adding child
        // nodes to our root nodes.
        // below lines is used to add languages
        // to our Language node. we are adding c++,
        // java to our language node.
        // below line isused to add arrays, 
        // strings to our dsa node. we are 
        // adding Arrays,Strings to our DSA node.
        // below line is used for adding sorting
        // algo to our Algorithm node.
        // below line is for setting our root node.
        // Inside our root node we are passing "root"
        // as our root node.


TreeView Output

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!