Search code examples
androidchartsandroidplot

What is the recommended method to create a speed meter graph in Android?


I'd like to create a chart similar to this

enter image description here

I'm considering using Android's 2-D graphics API with the classes android.graphics.Canvas and android.graphics.Paint for doing that, using Canvas's method drawLine() to draw the segments on the border.

Alternatively, is there a chart libary that could help programming this kind of chart ?


Solution

  • You can use SpeedView, it has several different types of speedometers:

    enter image description here

    To make it work, add the dependency to your build.gradle file:

    compile 'com.github.anastr:speedviewlib:1.1.7'
    

    Then simply add view to your XML layout:

    <com.github.anastr.speedviewlib.SpeedView
        android:id="@+id/speedView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    

    You can set speed on gauge with the next code:

    SpeedView speedometer = findViewById(R.id.speedView);
    speedometer.speedTo(50);