2

Requirement:

Add Pie Chart in a listView.

Approach Taken: Create a custom list Adapter and have a linearlayout which is a place holder for Pie Chart. Use achartEngine to create Pie Chart during run time and attach it to the place holder.

Problem: Now when I scroll the size of Pie Chart changes which is not desired.

Reasons could be that since listview generates the view dynamically the size available for the LinearLayout in the Custom Layout of adapter changes while scrolling and hence when I attach the pie chart view using fill_parent parameter, the size varies.

-- EDIT --

Layout for Adapter

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#A0FAF8CC"
    android:orientation="horizontal"
    android:paddingBottom="5dp"
    android:paddingTop="5dp"
    android:baselineAligned="false" >

<LinearLayout
    android:layout_width="0px"
    android:layout_height="wrap_content"
    android:layout_weight="0.5"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/title"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:ellipsize="end"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:text="@string/shopping"
        android:textColor="@android:color/black"
        android:textStyle="bold" />

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:gravity="left"
            android:text="@string/budget"
            android:paddingLeft="10dp"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/allTotalTypeBudget"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="right"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:gravity="left"
            android:paddingLeft="10dp"
            android:text="@string/expense"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/expenseTotalTypeBudget"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="right"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1.0"
        android:orientation="horizontal" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="top"
            android:gravity="left"
            android:paddingLeft="10dp"
            android:text="@string/remaining"
            android:textColor="@android:color/black" />

        <TextView
            android:id="@+id/remainingTotalTypeBudget"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:ellipsize="end"
            android:gravity="right"
            android:textColor="@android:color/black"
            android:textStyle="bold" />
    </LinearLayout>
</LinearLayout>

<LinearLayout
    android:id="@+id/chartView"
    android:layout_width="0px"
    android:layout_height="fill_parent"
    android:layout_weight="0.50"
    android:clickable="false"
    android:gravity="center" >
</LinearLayout>
</LinearLayout>
4
  • It would help if you showed the layout parameters of your custom list item.
    – Pedantic
    Feb 21, 2012 at 17:38
  • Done...please check the layout.
    – PravinCG
    Feb 21, 2012 at 17:59
  • One last question - what have you done to deviate from this layout, and did anything you've hacked at change anything? I have a similar group of nested linear layouts (in various directions) that works out ok, but it took some tweaking. What have you tried to tweak?
    – Pedantic
    Feb 28, 2012 at 4:40
  • I have tried few things, like giving the chartView a fixed size, changing the layout weights and also resorting to RelativeLayout with chart being drawn first followed by the texts, did not help.Can you tell me the tweaks you did?
    – PravinCG
    Feb 28, 2012 at 5:58

2 Answers 2

4
+50

Try this

renderer.setInScroll(true);

docs describe it as "To be set if the chart is inside a scroll view and doesn't need to shrink when not enough space."

1
  • This does it. I had set it to false thinking that it would stop the inview scroll of the chart. Definitely need to pay attention to the docs. Thank you!
    – PravinCG
    Feb 28, 2012 at 6:29
0

could you have a try of these? changed the root LinearLayout and the the first column layout_height to warp_content, then give the chartView a fix width & height & remove the android:layout_weight attr

<LinearLayout
   android:layout_width="fill_parent"
   android:layout_height="warp_content" 
   android:background="#A0FAF8CC"
   android:orientation="horizontal"
   android:paddingBottom="5dp"
   android:paddingTop="5dp"
   android:baselineAligned="false" >

   <LinearLayout
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_weight="1"
      android:orientation="vertical" >

<TextView
    android:id="@+id/title"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:ellipsize="end"
    android:gravity="left"
    android:paddingLeft="10dp"
    android:text="@string/shopping"
    android:textColor="@android:color/black"
    android:textStyle="bold" />

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="left"
        android:text="@string/budget"
        android:paddingLeft="10dp"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/allTotalTypeBudget"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="right"
        android:textColor="@android:color/black"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:text="@string/expense"
        android:textColor="@android:color/black" />

    <TextView
        android:id="@+id/expenseTotalTypeBudget"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="right"
        android:textColor="@android:color/black"
        android:textStyle="bold" />
</LinearLayout>

<LinearLayout
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_weight="1.0"
    android:orientation="horizontal" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:gravity="left"
        android:paddingLeft="10dp"
        android:text="@string/remaining"
        android:textColor="@android:color/black" />

     <TextView
        android:id="@+id/remainingTotalTypeBudget"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:ellipsize="end"
        android:gravity="right"
        android:textColor="@android:color/black"
        android:textStyle="bold" />
   </LinearLayout>
</LinearLayout>

<LinearLayout
android:id="@+id/chartView"
 android:layout_width="xxxdp"
android:layout_height="XXXdp"

android:clickable="false"
android:gravity="center" >
</LinearLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.