Set width and height for mpandroidchart in ListView

3.9k Views Asked by At

I have a ListView that show charts (barchart MPAndroidChart) in it, my layout code is this:

 <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent" android:layout_height="match_parent"
    android:orientation="vertical"
    android:id="@+id/linlayme">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:text="Medium Text"
        android:id="@+id/reportname"
        android:layout_gravity="center_horizontal"
        android:paddingTop="8dp"
        android:paddingBottom="8dp" />

    <com.github.mikephil.charting.charts.BarChart
        android:id="@+id/chart"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

In run time it does not show any chart. When defining width and height like 200dp charts shows, how can I set the size of the bar chart to match the parent?

2

There are 2 best solutions below

0
On

You have to set the height

(This layout is used in the example of MpAndroidListViewChart)

 <?xml version="1.0" encoding="utf-8"?>    
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    
          android:layout_width="match_parent"    
          android:layout_height="wrap_content"    
          android:orientation="vertical" >
        <com.github.mikephil.charting.charts.LineChart
             android:id="@+id/chart"
                android:layout_width="match_parent"
                android:layout_height="200dp" />
     </LinearLayout>
0
On

As I worked with MPAndroidChart before,I don't think the problem with your chart is just the height cause you said it doesn't show any chart there , But If you think so you should set height and width of your parent layout which here is :

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" 
android:layout_height="200dp"
android:orientation="vertical"
android:id="@+id/linlayme">
....
</LinearLayout>

If the problem persists after this ,please post your chart java code so we can provide more help.