'MPandroidChart Layout Height
I am using the MPAndroidChart to create a bar chart inside a fragment, but the chart height is only a fraction of the total screen. Also, when the height is set to a number, it works properly, but that is not the solution that I want. What is the problem? Here is the fragment layout code below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity$PlaceholderFragment">
<com.github.mikephil.charting.charts.BarChart
android:id="@+id/barchart"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
EDIT: Sorry for the lack of details, I used the Tabbed Activity with Action Bar Spinner template for my project.
<android.support.design.widget.AppBarLayout android:id="@+id/appbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="@+id/toolbar"
android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways">
<Spinner android:id="@+id/spinner" app:popupTheme="@style/AppTheme.PopupOverlay"
android:layout_width="wrap_content" android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView android:id="@+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />
Also I have the image of the application running
Solution 1:[1]
I know this is an older question, but I ran into the same problem - my programmatically created chart was wide as needed, but the height wasn't being inherited from the parent linearLayout.
The solution involved setting the height programmatically after adding it to the view of the parent, then redrawing it (invalidate()).
Layout:
<LinearLayout
android:id="@+id/short_performance_chart"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical"
android:layout_marginTop="20dp"
android:background="@drawable/graph_gradient"/>
Code:
LinearLayout chartHolder = (LinearLayout) getActivity().findViewById(R.id.short_performance_chart);
BarChart mChart = (BarChart) initializeBarChart(); //just populating data, etc
chartHolder.addView(mChart);
// Due to the fact that we are in a fragment we have to reset the height
mChart.getLayoutParams().height = android.view.ViewGroup.LayoutParams.MATCH_PARENT;
mChart.invalidate();
Solution 2:[2]
Wrap chart with a FrameLayout
<FrameLayout
android:id="@+id/fl_lineChartImage"
android:layout_width="match_parent"
android:layout_height="250dp"
>
<com.github.mikephil.charting.charts.LineChart
android:id="@+id/lineChartImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</FrameLayout>
Then chage the size of the FrameLayout dynamically
val imageChart: LineChart =findViewById(R.id.lineChartImage)
fl_imageChart: FrameLayout =findViewById(R.id.fl_lineChartImage)
val params: ViewGroup.LayoutParams = fl_imageChart.getLayoutParams()
params.width = FrameLayout.LayoutParams.MATCH_PARENT
params.height = 100 //enter size here
fl_imageChart.setLayoutParams(params)
imageChart.invalidate()
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Justin |
| Solution 2 |
