Open In App

Android – Line Graph View with Kotlin

Last Updated : 30 Jun, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

Graphs are used in android applications to display vast amounts of data in an easily readable form. There are different types of graphs used such as BarGraph, GroupBar graph, point, and line graph to represent data. In this article, we will take a look at How to use Line Graph View in Android using Kotlin. A sample video is given below to get an idea about what we are going to do in this article.

Note: If you are looking to implement Line Graph View in Android using Java. Check out the following article: Line Graph View in Android using Java

Step by Step Implementation

Step 1: Create a New Project in Android Studio

To create a new project in Android Studio please refer to How to Create/Start a New Project in Android Studio. Note that select Kotlin as the programming language.

Step 2: Add dependency to the build.gradle(Module:app) file

Navigate to the Gradle Scripts > build.gradle(Module:app) and add the below dependency in the dependencies section.  

implementation 'com.jjoe64:graphview:4.2.2'    

After adding this dependency sync your project and now we will move towards its implementation.  

Step 3: 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"?>
<!--on below line we are creating a swipe to refresh layout-->
<RelativeLayout 
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
    android:orientation="vertical"
    tools:context=".MainActivity">
  
    <!--text view for displaying heading-->
    <TextView
        android:id="@+id/idTVHead"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_margin="4dp"
        android:gravity="center"
        android:padding="4dp"
        android:text="@string/app_name"
        android:textAlignment="center"
        android:textColor="@color/purple_200"
        android:textSize="20sp"
        android:textStyle="bold" />
  
    <!--line graph view where we will
        be displaying our data-->
    <com.jjoe64.graphview.GraphView
        android:id="@+id/idGraphView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_below="@id/idTVHead" />
  
</RelativeLayout>


Step 4: Working with the MainActivity.kt file

Navigate to app>java>your app’s package name>MainActivity.kt file and add the below code to it. Comments are added in the code to get to know in detail. 

Kotlin




package com.gtappdevelopers.kotlingfgproject
  
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import com.jjoe64.graphview.GraphView
import com.jjoe64.graphview.series.DataPoint
import com.jjoe64.graphview.series.LineGraphSeries
  
class MainActivity : AppCompatActivity() {
  
    // on below line we are creating 
    // variables for our graph view
    lateinit var lineGraphView: GraphView
  
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
          
        // on below line we are initializing
        // our variable with their ids.
        lineGraphView = findViewById(R.id.idGraphView)
  
        // on below line we are adding data to our graph view.
        val series: LineGraphSeries<DataPoint> = LineGraphSeries(
            arrayOf(
                // on below line we are adding
                // each point on our x and y axis.
                DataPoint(0.0, 1.0),
                DataPoint(1.0, 3.0),
                DataPoint(2.0, 4.0),
                DataPoint(3.0, 9.0),
                DataPoint(4.0, 6.0),
                DataPoint(5.0, 3.0),
                DataPoint(6.0, 6.0),
                DataPoint(7.0, 1.0),
                DataPoint(8.0, 2.0)
            )
        )
  
        // on below line adding animation
        lineGraphView.animate()
  
        // on below line we are setting scrollable
        // for point graph view
        lineGraphView.viewport.isScrollable = true
  
        // on below line we are setting scalable.
        lineGraphView.viewport.isScalable = true
  
        // on below line we are setting scalable y
        lineGraphView.viewport.setScalableY(true)
  
        // on below line we are setting scrollable y
        lineGraphView.viewport.setScrollableY(true)
  
        // on below line we are setting color for series.
        series.color = R.color.purple_200
  
        // on below line we are adding
        // data series to our graph view.
        lineGraphView.addSeries(series)
    }
}


Now run your application to see the output of it. 

Output:



Similar Reads

Line Graph View in Android with Example
If you are looking for a view to represent some statistical data or looking for a UI for displaying a graph in your app then in this article we will take a look on creating a line graph view in our Android App. What we are going to build in this article? We will be building a simple Line Graph View in our Android app and we will be displaying some
3 min read
How to Create Your Own Custom View in Android with Kotlin?
In this article, we're going to talk about how we can create our own custom view in Android step by step. We all know that in the beginning android platform provides us some basic views for example - TextView, ImageView, EditText, Button, ImageButton, RadioButton, etc. But, sometimes we need a whole new type of view where our prebuilt layouts and w
4 min read
Calendar View App in Android with Kotlin
Calendar View is seen in most travel booking applications in which the user has to select the date of the journey. For the selection of the date, this view is used. In this article, we will take a look at How to implement Calendar View within our Android application using Kotlin. A sample video is given below to get an idea about what we are going
3 min read
Android View Shaker in Kotlin
View Shaker is an android animation in which the UI of the screen vibrates for a specific interval of time. We can implement this View shaker to the specific views of the application. View Shaker provides different animation effects which we can add to our views such as bounce, fade, float, and others. In this article, we will take a look at implem
4 min read
Android Scratch Card View using Kotlin
Scratch Card View is nowadays seen in most of the payment applications which provide coupons on various transactions within our application. This scratch card view is used to scratch the coupon and display the coupon to the user. In this article, we will take a look at How to Create a simple Scratch Card View within our android application using Ko
3 min read
How to Draw a Line in Android with Kotlin?
In Android, Canvas is a class that performs drawings in two-dimension space (X and Y on the screen) on a Bitmap. This Bitmap can then be displayed using an ImageView. In this article, we will show you how you could draw lines using canvas, store them on Bitmap and display it in an ImageView in Android. Follow the below steps once the IDE is ready.
3 min read
Android - Point Graph Series with Kotlin
Graphs are used to represent vast amounts of data in an easily readable form. There are several graphs used to display data such as simple bar graphs, group bar graphs, point graph series, and others. In this article, we will take a look at the implementation of Point Graph Series in Android applications using Kotlin. Note: If you are looking to im
3 min read
Creating a Anagram Checker Android App in Android Studio with Kotlin
An anagram of a word is another word that contains the same characters, only the order of characters can be different. For example, “abcd” and “dabc” are an anagram of each other. In this article, we will be building an Anagram Checker android app in Android Studio using Kotlin and XML. The app will check whether the entered words are anagrams of e
3 min read
Kotlin Environment setup for Command Line
To set up a Kotlin environment for the command line, you need to do the following steps: Install the Java Development Kit (JDK): Kotlin runs on the Java virtual machine, so you need to have the JDK installed. You can download the latest version from the official Oracle website.Download the Kotlin compiler: You can download the latest version of the
2 min read
Android - Create Group BarChart with Kotlin
Bar Charts in android are used to represent vast amounts of data in an easily readable format. We can display a single bar chart within our bar chart. But for making a comparison between two categories we can implement a group bar chart to display data for both categories side by side. In this article, we will be building a simple Group Bar Chart i
5 min read
Article Tags :