MP PhilJay Pie Chart

android MP Philjat Pie chart

Android MP PhilJay Pie Chart Android Studio

This tutorial describes how to create Android MP PhilJay Pie Chart Android Studio with MP PhilJay liberary and how to use bar entry from array list. and also describes how to create onClickListenter to show each Pie chart value in alert dialog. also, you will learn how design and show data for each pie slice. like setting the legend, colors, setting X and Y value position. what ever you need inside slice or outside the slice.

Android mp philjay charts library is jitpack library which is maven repositories. So, it requires dependency. Lets start.

MP Philjay Bar Chart Dependencies

Start a new android studio and open project level gradle file, then add the jitpack maven dependency.

android project level gradle file
Open project level gradle file maven dependency
add maven dependency in allprojects
maven { url "" }

Then, open module level gradle file and add the following dependencies. And click sync.

android module level gradle file
open module level gradle file
mp philjay charts dependencies
add mp philjay dependency
implementation ''
implementation 'com.github.PhilJay:MPAndroidChart:v3.0.3'

Pie Chart XML Layout

The next step, Open main activity xml file and add android mp philjay bar chart.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android=""


android mp philijay pie chart xml file

Then, create new java class the will represent the model of sales per region.

public class RegionalSalesData {
String region;
int sales;

public RegionalSalesData(String region, int sales) {
this.region = region;
this.sales = sales;

public String getRegion() {
return region;

public void setRegion(String region) {
this.region = region;

public int getSales() {
return sales;

public void setSales(int sales) {
this.sales = sales;

Open main activity java file and create general objects of PieChart, arrayList of PieEntries. And initiate them in onCreate method. Create ArrayList of RegionalSalesData class.

pie chart android studio
public class MainActivity extends AppCompatActivity {
PieChart pieChart;
ArrayList<PieEntry> pieEntries;
ArrayList<RegionalSalesData> regionalSalesDataArrayList =new ArrayList<>();

Next, we need to create the method that file the RegionalSalesArrayList with data the the pie chart will show it.

private void fillRegionalSalesArrayList(){

regionalSalesDataArrayList.add(new RegionalSalesData("Alex",242000));
regionalSalesDataArrayList.add(new RegionalSalesData("Cairo",300000));
regionalSalesDataArrayList.add(new RegionalSalesData("Suez",150000));
regionalSalesDataArrayList.add(new RegionalSalesData("Upper EGypt",200000));

And call the method in onCreate method of main activity.


The PieEntryArrayList Step, we need to loop through RegionalSalesArrayList and add each sales of the month to pieEntryList. Each PieEntry object requires two parameters. The first is float value(sales) and the second is String label(region).

for (int i =0; i < regionalSalesDataArrayList.size();i++){
String region = regionalSalesDataArrayList.get(i).getRegion();
int sales = regionalSalesDataArrayList.get(i).getSales();
pieEntries.add(new PieEntry(sales,region));

You are a hero, next we need to create a new PieDataSet object that takes two parameters. The first is list of PieEntry list and the second is String Label name. You can set the colors of pie Chart through pieDataset.setColors() method. To set name of regions outside the slice through setXvaluePosition() and setYValuePosition() methods of pie chart. To set text size of sales use setValueTextSize() method.

PieDataSet pieDataSet = new PieDataSet(pieEntries,"Regional Sales");

Very Good, next create new PieData Object and cast pieDataSet to it, and cast the pieData to the pieChart.

PieData pieData = new PieData(pieDataSet);

MP Philjay Chart Legend

The chart legend represent the names of regions with colored square which determine the color of each region on the pie chart slice. see next image.

mp philjay charts legend
pie chart legend
Legend legend = pieChart.getLegend();

You are a hero android developer , Lets run your app to show the chart.

android mp philjay pie chart

Pie chart Android set on Pie value selected listener

pieChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
public void onValueSelected(Entry e, Highlight h) {
int x = pieChart.getData().getDataSetForEntry(e).getEntryIndex((PieEntry)e);
String region = regionalSalesDataArrayList.get(x).getRegion();
String sales = NumberFormat.getCurrencyInstance().format(regionalSalesDataArrayList.get(x).getSales());
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View view = LayoutInflater.from(MainActivity.this).inflate(R.layout.regional_sales_layout,null);
TextView regionTxtView = view.findViewById(;
TextView salesTxtView = view.findViewById(;
alertDialog = builder.create();;


public void onNothingSelected() {


Thank You

Tutorials You may interested in.

Related posts

Leave a Comment