Skip to content
Related Articles

Related Articles

Improve Article

TreeView in Android with Example

  • Last Updated : 18 Feb, 2021
Geek Week

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

Example

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.

XML




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

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">
      
    <!--Below is the code for tree view-->
    <de.blox.treeview.TreeView
        android:id="@+id/idTreeView"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />
  
</RelativeLayout>

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




<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
  
    <TextView
        android:id="@+id/idTvnode"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="5dp"
        android:background="@color/purple_500"
        android:padding="8dp"
        android:text="@string/my_node"
        android:textColor="@color/white"
        android:textSize="20sp"
        android:textStyle="bold" />
  
</RelativeLayout>

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 Viewholder.java file. 

Java




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

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

Step 7: Working with the MainActivity.java file

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

Java




import android.os.Bundle;
import android.view.View;
  
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
  
import de.blox.treeview.BaseTreeAdapter;
import de.blox.treeview.TreeNode;
import de.blox.treeview.TreeView;
  
public class MainActivity extends AppCompatActivity {
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
          
        // creating a variable for tree view.
        TreeView treeView = findViewById(R.id.idTreeView);
          
        // 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) {
            @NonNull
            @Override
            public Viewholder onCreateViewHolder(View view) {
                return new Viewholder(view);
            }
  
            @Override
            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.
                viewHolder.textView.setText(data.toString());
  
            }
        };
          
        // below line is setting adapter for our tree.
        treeView.setAdapter(adapter);
          
        // 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.
        root.addChild(DSAchildNode);
        root.addChild(languageNode);
        root.addChild(AlgoChildNode);
          
        // below lines is used to add languages
        // to our Language node. we are adding c++,
        // java to our language node.
        languageNode.addChild(CchildNode);
        languageNode.addChild(javaChildNode);
          
        // below line isused to add arrays, 
        // strings to our dsa node. we are 
        // adding Arrays,Strings to our DSA node.
        DSAchildNode.addChild(arrayChild);
        DSAchildNode.addChild(stringChild);
          
        // below line is used for adding sorting
        // algo to our Algorithm node.
        AlgoChildNode.addChild(sortingChildNode);
  
        // below line is for setting our root node.
        // Inside our root node we are passing "root"
        // as our root node.
        adapter.setRootNode(root);
    }
}

Output:

TreeView Output

Want a more fast-paced & competitive environment to learn the fundamentals of Android?
Click here to head to a guide uniquely curated by our experts with the aim to make you industry ready in no time!



My Personal Notes arrow_drop_up
Recommended Articles
Page :