android mp philjay bart chart android studio

MP PhilJay Bar Chart Android

Android MP PhilJay Bar Chart Android Studio

This tutorial describes how to create Android MP PhilJay Bar 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 bar in the chart value in alert dialog.

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. maven dependency
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.

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

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

BarChart XML Layout

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

mp philjay bar chart xml layout

Open main activity java file and create general objects of BarChart, arrayList of BarEntries and arrayList of String that will represent the label name of the months. And initiate them in onCreate method.

android bar chat
BarChart barChart;
ArrayList<BarEntry> barEntriesArrayList;
ArrayList<String > lableName;
android bar chart
barChart = findViewById(;
barEntriesArrayList = new ArrayList<>();
lableName  = new ArrayList<>();

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

public class MonthlySalesData {
String month;
int sales;

public MonthlySalesData(String month, int sales) {
this.month = month;
this.sales = sales;

public String getMonth() {
return month;

public void setMonth(String month) {
this.month = month;

public int getSales() {
return sales;

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

Bar chart data step, create general arrayList of MonthlySalesData and initialize it in main activity class.

public class MainActivity extends AppCompatActivity {

BarChart barChart;
ArrayList<BarEntry> barEntriesArrayList;
ArrayList<String > lableName;
ArrayList<MonthlySalesData> monthlySalesDataArrayList = new ArrayList<>();

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

private void fillMonthlySalesArrayList(){

monthlySalesDataArrayList.add(new MonthlySalesData("Jan",242000));
monthlySalesDataArrayList.add(new MonthlySalesData("Feb",300000));
monthlySalesDataArrayList.add(new MonthlySalesData("Mar",150000));
monthlySalesDataArrayList.add(new MonthlySalesData("Apr",250000));
monthlySalesDataArrayList.add(new MonthlySalesData("May",242000));
monthlySalesDataArrayList.add(new MonthlySalesData("June",300000));
monthlySalesDataArrayList.add(new MonthlySalesData("July",150000));
monthlySalesDataArrayList.add(new MonthlySalesData("Aug",210000));
monthlySalesDataArrayList.add(new MonthlySalesData("Sep",242000));
monthlySalesDataArrayList.add(new MonthlySalesData("Oct",320000));
monthlySalesDataArrayList.add(new MonthlySalesData("Nov",150000));
monthlySalesDataArrayList.add(new MonthlySalesData("Dec EGypt",200000));

And call the method in onCreate method of main activity.


The BarEntryArrayList Step, we need to loop through monthlySalesArrayList and add each sales of the month to barEntryList. And in the same loop add month name in label nameArrayList. As following code.

for (int i =0; i < monthlySalesDataArrayList.size();i++){
String month = monthlySalesDataArrayList.get(i).getMonth();
int sales = monthlySalesDataArrayList.get(i).getSales();
barEntriesArrayList.add(new BarEntry(i,sales));

Good work, next we need to create a new BarDataSet object that takes two parameters. The first is list of BarEntry and the second is String Label name. You can set the colors of Bar Chart through barDataset.setColors() method.

BarDataSet barDataSet = new BarDataSet(barEntriesArrayList,"Monthly Sales");

Bar Chart Description, You can create bar chart description and set it`s text, position and text alignment. And cast it to bar chart description.

Description description = new Description();

Very Good, next create new BarData Object and cast barDataSet to it, and cast the barData to the barChart.

BarData barData = new BarData(barDataSet);

Bar Chart XAXIS Formatter

Create a new object of XAxis which is getMethod of barChart object. The xAxis shows the label names(month names) and make us to set the positions, grid lines, granularity, label count and label rotating angle.

xAxis.setValueFormatter(new IndexAxisValueFormatter(lableName));

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

android mp philjay bar chart

Android MP PhilJay Bar Chart Click Listener

barChart.setOnChartValueSelectedListener(new OnChartValueSelectedListener() {
public void onValueSelected(Entry e, Highlight h) {
int x = barChart.getData().getDataSetForEntry(e).getEntryIndex((BarEntry) e);
String region = monthlySalesDataArrayList.get(x).getMonth();
String sales = NumberFormat.getCurrencyInstance().format(monthlySalesDataArrayList.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() {

android bar chart value selected listener

Thank You

Tutorials You may interested in.

Leave a Comment

Your email address will not be published. Required fields are marked *