Open jarinrocks opened 3 years ago
private fun getThemeTextColor(): Int {
val typedValue = TypedValue()
val theme = this.requireContext().theme
theme.resolveAttribute(android.R.attr.textColor, typedValue, true)
return typedValue.data
}
private fun initUI() {
val color = getThemeTextColor()
binding.lineChart.data = viewModel.getLineData()
binding.lineChart.data.setValueTextColor(color)
binding.lineChart.xAxis.textColor= color
binding.lineChart.axisLeft.textColor = color
binding.lineChart.axisRight.textColor = color
binding.lineChart.legend.textColor = color
//pie chart
binding.pie.data.setValueTextColor(color)
binding.pie.legend.textColor = color
}
this work fine to me, i assume color should be same as textcolor, because it should be clear from background, however you can custom in theme if you prefer
@Kenny50 Thank you. I will try this.
Here's a java version that worked better for me pulling from theme
public static int getThemeTextColor(Context context) {
TypedValue typedValue = new TypedValue();
Resources.Theme theme = context.getTheme();
theme.resolveAttribute(R.attr.colorOnSecondary, typedValue, true);
@ColorInt int color = typedValue.data;
return color;
}
and
int color = App.getThemeTextColor(requireContext());
chart.getData().setValueTextColor(color);
chart.getData().setValueTextColor(color);
chart.getXAxis().setTextColor(color);
chart.getAxisLeft().setTextColor(color);
chart.getAxisRight().setTextColor(color);
chart.getLegend().setTextColor(color);
When I changed theme to dark mode the text color of the the texts in the Pie Chart is not getting changed in accordance with the dark mode.
Device : Moto G4 Android : Nougat(7.0) MPAndroidChart Version : 2.2.4