Android Drawing Line Chart in a Custom XML Layout using AChartEngine

October 22, 2012

In this article, we will create a line chart in a custom layout where as in our previous article titled “Android Drawing Line Chart using AChartEngine“, we have created line chart in an independent activity using AChartEngine library.

This application is developed in Eclipse (4.2.0) with ADT plugin ( 20.0.3 ) and Android SDK ( R20.0.3 ).

1. Create an Android application project namely “AChartEngineLineChartCustomLayout”

Create a new Android Application project namely "AChartEngineCustomLayout"

Figure 1 : Create a new Android Application project namely "AChartEngineCustomLayout"

2. Design application launcher icon

Design Application Launcher Icon

Figure 2 : Design Application Launcher Icon

3. Create a blank activity to define the class MainActivity

Create a blank activity

Figure 3 : Create a blank activity

4. Enter MainActivity details

Enter MainActivity Details

Figure 4 : Enter MainActivity Details

5. Delete Android Support Library from this project, if exists

By default Eclipse ( 4.2.0) adds Android Support Library to  Android application project. For this application, we don’t need to use this support library. So the library file libs/android-support-v4.jar may be removed manually via ProjectExplorer by simply right click on the file and then clicking the menu item “delete”.

6. Download the latest version of AChartEngine library ( jar file ) from here

7. Copy the downloaded library file to “libs” directory of the project

8. Update the file res/values/strings.xml

    <string name="app_name">AChartEngineLineChartCustomLayout</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">AChartEngine Library Demo</string>
    <string name="str_tv_title">AChartEngine Graph in a custom layout</string>

9. Update the layout of the MainActivity in the file res/layout/activity_main.xml

<RelativeLayout xmlns:android=""
    android:layout_height="match_parent" >

        android:gravity="center_horizontal" />

        android:layout_below="@id/tv_title" >

Note : The line chart will be added to chart_container at run time.

10. Update the file res/values/styles.xml

<style name="AppTheme" parent="android:Theme" />

11. Update the file res/values-v11/styles.xml

<style name="AppTheme" parent="android:Theme.Holo" />

12. Update the file res/values-v14/styles.xml

<style name="AppTheme" parent="android:Theme.Holo" />

13. Update the class MainActivity in the file src/in/wptrafficanalyzer/achartenginelinechartcustomlayout/

package in.wptrafficanalyzer.achartenginelinechartcustomlayout;

import org.achartengine.ChartFactory;
import org.achartengine.chart.PointStyle;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;

import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

    private View mChart;

    private String[] mMonth = new String[] {
        "Jan", "Feb" , "Mar", "Apr", "May", "Jun",
        "Jul", "Aug" , "Sep", "Oct", "Nov", "Dec"

    public void onCreate(Bundle savedInstanceState) {

    private void openChart(){
        int[] x = { 1,2,3,4,5,6,7,8 };
        int[] income = { 2000,2500,2700,3000,2800,3500,3700,3800};
        int[] expense = {2200, 2700, 2900, 2800, 2600, 3000, 3300, 3400 };

        // Creating an  XYSeries for Income
        XYSeries incomeSeries = new XYSeries("Income");
        // Creating an  XYSeries for Expense
        XYSeries expenseSeries = new XYSeries("Expense");
        // Adding data to Income and Expense Series
        for(int i=0;i<x.length;i++){
            incomeSeries.add(x[i], income[i]);

        // Creating a dataset to hold each series
        XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset();
        // Adding Income Series to the dataset
        // Adding Expense Series to dataset

        // Creating XYSeriesRenderer to customize incomeSeries
        XYSeriesRenderer incomeRenderer = new XYSeriesRenderer();

        // Creating XYSeriesRenderer to customize expenseSeries
        XYSeriesRenderer expenseRenderer = new XYSeriesRenderer();

        // Creating a XYMultipleSeriesRenderer to customize the whole chart
        XYMultipleSeriesRenderer multiRenderer = new XYMultipleSeriesRenderer();
        multiRenderer.setChartTitle("Income vs Expense Chart");
        multiRenderer.setXTitle("Year 2012");
        multiRenderer.setYTitle("Amount in Dollars");
        for(int i=0;i<x.length;i++){
            multiRenderer.addXTextLabel(i+1, mMonth[i]);

        // Adding incomeRenderer and expenseRenderer to multipleRenderer
        // Note: The order of adding dataseries to dataset and renderers to multipleRenderer
        // should be same

        // Getting a reference to LinearLayout of the MainActivity Layout
        LinearLayout chartContainer = (LinearLayout) findViewById(;

        // Creating a Line Chart
        mChart = ChartFactory.getLineChartView(getBaseContext(), dataset, multiRenderer);

        // Adding the Line Chart to the LinearLayout

    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(, menu);
        return true;

14. Run the application

LineChart displayed in a Custom Layout

Figure 5 : LineChart displayed in a Custom Layout

15. Download

16. Reference

How to hire me?

I am George Mathew, working as software architect and Android app developer at

You can hire me on hourly basis or on project basis for Android applications development.

For hiring me, please mail your requirements to

My other blogs

Android Knowledge Quiz

Ready to test your knowledge in Android? Take this quiz :

Tags: , , , ,

6 Responses to Android Drawing Line Chart in a Custom XML Layout using AChartEngine

  1. Shobhit Puri on March 19, 2013 at 3:38 pm

    It was reallly helpful. Thanks

  2. Murali on August 22, 2014 at 5:16 pm

    While i am running this same code inside fragment i am getting force close error at this line.


  3. Naveen on February 26, 2015 at 1:06 pm

    Hi sir,
    The layout whose containing the Graph Portion is Scrollable.Can i make it static.

  4. abh22ishek on November 5, 2015 at 3:51 pm

    how you plot the y axis value ?

  5. meysam on December 1, 2015 at 4:04 pm

    Hello good time
    How can I change my data anywhere in my size?
    How can I use this program in a fragment?
    Original background color was changed how?

  6. Sangita kankhar on March 2, 2016 at 11:53 am

    I have three points I want to draw triangle by connecting that points on finger touch.Will you please give me the example

Leave a Reply

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

Be friend at g+

Subscribe for Lastest Updates

FBFPowered by ®Google Feedburner