VOOZH about

URL: https://www.geeksforgeeks.org/android/how-to-add-a-pie-chart-into-an-android-application/

⇱ How to add a Pie Chart into an Android Application - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to add a Pie Chart into an Android Application

Last Updated : 12 Jul, 2025

A Pie Chart is a circular statistical graphic, which is divided into slices to illustrate numerical proportions. It depicts a special chart that uses “pie slices”, where each sector shows the relative sizes of data. A circular chart cuts in the form of radii into segments describing relative frequencies or magnitude also known as a circle graph. A pie chart represents numbers in percentages, and the total sum of all segments needs to equal 100%.

Here is the Final Application which will be created.

👁 Pie_Chart

Step-by-Step Implementation of Pie Chart in Android Application

So let’s see the steps to add a Pie Chart into an Android app.

Step 1: Opening/Creating 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: Select Java/Kotlin as the programming language.

Directory Structure for the Application will seem like this after creation:

👁 Directory_Structure


Step 2: Before going to the Coding Section first you have to do some Pre-Task.

i).

Add the dependencies required in the application to implement the Pie Chart.

build.gradle (Module: app):


After Importing following dependencies and click the “sync Now” on the above pop up.

ii).

colors.xml:


Step 3: Designing the Layout of the Application

Layout is the component which is essential to define the UI of the application , So let us edit the activity main file.

activity_main.xml:

Design UI:

👁 Layout_1


Step 4: Add the Functionality to the Application

Now We have created a fine layout implementing the Pie Chart. So, let us add the functionality to this layout and the application by working in MainActivity File.

MainActivity File:

Explanation of the above Program

  1. Create instance of TextViews and PieChart.
  2. Link those objects with their respective id's that we have given in .XML file
  3. Create a private void setData() method outside onCreate() method and define it.
  4. Inside setData() method the most important task is going to happen that is how we set the data in the text file and as well as on the piechart.
  5. First of all inside setData() method set the percentage of language used in their respective text view.
  6. And then set this data to the pie chart and also set their respective colors using addPieSlice() method.
  7. For better look animate the piechart using startAnimation().
  8. At last invoke the setData() method inside onCreate() method.

Note : To access the full android application using Pie chat check this repository: Pie Chart Android Application

Running the Application:


Comment

Explore