1

I wanted to use MPAndroidChartLibrary in my application, but I am experiencing problems which I can't solve on my own. I would like to put the chart into the CardView which is stored inside ListView. CardView's xml looks like this:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >
    <android.support.v7.widget.CardView
        xmlns:card_view="http://schemas.android.com/apk/res-auto"
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/card_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        card_view:cardCornerRadius="2dp"
        card_view:contentPadding="16dp"
        card_view:cardUseCompatPadding="true"
        android:layout_marginLeft="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >
            <TextView
                android:id="@+id/textDateStartTitle"
                android:paddingLeft="5dp"
                android:textSize="26sp"
                android:textColor="@color/accent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:gravity="left"/>

            <com.github.mikephil.charting.charts.PieChart
                android:id="@+id/pie_chart"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

        </LinearLayout>
    </android.support.v7.widget.CardView>
</FrameLayout>

When opening the fragment containing these CardViews I get an error:

java.lang.IllegalArgumentException: width and height must be > 0 at android.graphics.Bitmap.createBitmap(Bitmap.java:808) at android.graphics.Bitmap.createBitmap(Bitmap.java:787) at android.graphics.Bitmap.createBitmap(Bitmap.java:754) at com.github.mikephil.charting.charts.Chart.onSizeChanged(Chart.java:2197)

When I set android:layout_width and android:layout_height to some fixed values (eg. 200dp) inside com.github.mikephil.charting.charts.PieChart the chart looks ok and there is no error. How to get rid of this error while having width and height depending on the parent/content?

fragon
  • 3,391
  • 10
  • 39
  • 76

2 Answers2

3

Right now, your chart height does not depend on the height of its parent LinearLayout:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            />

Try this instead:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/pie_chart"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            />

The second set of attributes will allow your PieChart to fill all the vertical height available inside the container LinearLayout (after accounting for the height of the TextView above it).

stkent
  • 19,772
  • 14
  • 85
  • 111
  • Unfortunetaly this does not work either. The same error. I get no error only when I set `layout_heigt` and `layout_width` to some FIXED value, so I suspect that `match_parent` value is taken by the PieChart library before it is set to the real parent value. However I don't know how to fix it. – fragon Jan 09 '15 at 00:37
  • What happens if you use the code I suggested, and in addition, give the outer FrameLayout a fixed height in dp? – stkent Jan 09 '15 at 00:45
  • 1
    Oh, also, do you get the same error if the width is `match_parent` but the height is a fixed dp value? Or must both be dp values to avoid the problem? – stkent Jan 09 '15 at 00:46
  • In both cases you mentioned it works without an error. – fragon Jan 09 '15 at 00:52
  • Are either of those acceptable solutions? It makes sense that `wrap_content` in either dimension is not usable by the pie chart - it probably scales to match its size, so has no 'built-in' height by default. If you want to get fancy, you could put it in a container whose aspect ratio you fix by overriding onMeasure, but that's a lot more work than specifying a fixed dp height. – stkent Jan 09 '15 at 00:57
  • I've set a fixed height and it works as I wanted. So thank you for your help. – fragon Jan 09 '15 at 16:53
0

The only way I found was to attach the saving of file to gallery in onclick of a button. It works perfectly after that. I guess Once the view is fully drawn, the system have correct height and width.

chetan
  • 411
  • 1
  • 5
  • 8