Search code examples
androidlayoutviewachartenginefill-parent

AChartEngine - GraphicalView always match parent


I created a GraphicalView (a CombinedXYChartView) and added it to my layout. At next, I would like to add some CheckBoxes below my GraphicalView.

The problem is, that the GraphicalView always match parent and that's why I can't see my CheckBoxes (because there is no space for them).

Here is a screenshot:

Screenshot

And here is my code (snipped):

layout_chart.xml

<?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:background="@color/background_color"
android:orientation="vertical" >

<!-- layout for chart -->
<LinearLayout
    android:id="@+id/linearLayout_chart"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>

<!-- layout for checkboxes -->
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal" >

    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="checkBox1" />
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="checkBox2" />
</LinearLayout>
</LinearLayout>

FragmentChart.java

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    LinearLayout v = (LinearLayout) inflater.inflate(R.layout.layout_chart, container, false);
    graphicalView = initChart();  // Creating CombinedXYChartView
    LinearLayout layout = (LinearLayout) v.findViewById(R.id.linearLayout_chart);
    layout.addView(graphicalView, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
    return v;
}

The chart works perfect. Also the checkboxes, if I disable the chart. So, what's the problem?


Solution

  • I am thinking you will need to add weights to let the chart know that it needs to share some space. Update your xml to have a weight attribute of 1 in the linear layout containing the checkboxes.

    <?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:background="@color/background_color"
         android:orientation="vertical" >
    
      <!-- layout for chart -->
     <LinearLayout
         android:id="@+id/linearLayout_chart"
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:orientation="vertical" >
     </LinearLayout>
    
     <!-- layout for checkboxes -->
     <LinearLayout
         android:layout_width="match_parent"
         android:layout_height="wrap_content"
         android:layout_weight="1"
         android:orientation="horizontal" >
    
    <CheckBox
        android:id="@+id/checkBox1"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="checkBox1" />
    <CheckBox
        android:id="@+id/checkBox2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:checked="true"
        android:text="checkBox2" />
    </LinearLayout>
     </LinearLayout>