Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MPAndroidChart inside HorizontalScrollView

(this question is also valid for ScrollView)

I need to place a LineChart inside HorizontalScrollView. I do not wish to scroll inside the chart (meaning the chart renders the scroll itself and after multiple tesst, it's quite laggy) unless it is the last solution.

The library is MPAndroidChart.

Based on this post on Telerik it should not be complicated.

Unfortunately I cannot seem to achieve the aimed result.

This is my XML code :

<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.github.mikephil.charting.charts.LineChart
        android:layout_width="2000dp"
        android:layout_height="match_parent"
        android:fillViewport="true"
        android:id="@+id/chart"/>

</HorizontalScrollView>

Nothing really fancy.

Android Studio internal designer shows a really shrinked chart and my phone does too.

What is wrong ?

Thanks for the help!

like image 348
Mackovich Avatar asked Mar 02 '26 20:03

Mackovich


1 Answers

Use a LinearLayout as a container for your Line chart:

                    <HorizontalScrollView
                    android:id="@+id/hsvChart"
                    android:layout_width="800dp"
                    android:layout_height="match_parent">
                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent">
                        <com.github.mikephil.charting.charts.LineChart
                            android:id="@+id/chart"
                            android:layout_width="600dp"
                            android:layout_height="match_parent"
                            android:layout_marginBottom="3dp"
                            android:layout_marginTop="3dp" />
                    </LinearLayout>
                </HorizontalScrollView>

This will prevent your chart from shrinking

like image 140
Alldb Avatar answered Mar 05 '26 10:03

Alldb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!