0

I have a fragment where I have MPAndroidChart and textViews. I understand that MPAndroidChart doesn't support Data Binding as indicated in here: https://github.com/PhilJay/MPAndroidChart/issues/1382

Right now I am using ButterKnife to find my views, but want to migrate to Data Binding. I was wondering since that MPAndroidChart doesn't support Data Binding, is it possible to use both Data Binding and Butterknife in the same fragment ?

Butterknife for MPAndroidChart and DataBinding for my textviews.

4 Answers4

0

Yes it is possible,

Butterknife and Databinding are two different things.

But why you want butterknife. See below

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

You can access this field like this

ActivityMainBinding binding;
binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
binding.pieChart.setVisibility(Visibility.INVISIBLE)

So no need of butterknife when you use data binding.

Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
0

You can use PieChart with data binding by following code 1. Bind your layout with fragmnet

setPieChart(fragmentDashboardBinding.piechart);

Declare Pie chart variable global

private PieChart piechart;   
    public void setPieChart(PieChart Pie_chart) {
            this.piechart = Pie_chart;
        }

And Set your data on PieChart form Update Method

@Override
    public void update(Observable observable, Object arg) {

        DashboardViewModel dashboardViewModel = (DashboardViewModel) observable;
        drivingScore = dashboardViewModel.getDrivingScore();
        LoadPieChart(drivingScore);
    }

private void LoadPieChart(DrivingScore drivingScore) {
//Do any thing with your piechart
}
Divyanshu
  • 462
  • 6
  • 17
0

It is possible to use MPAndroidChart with databinding.

Create a custom binding adapter:

@BindingAdapter("android:setPieData")
fun setPieData(view: PieChart, data: List<SomeModel>?) {
val entries = mutableListOf<PieEntry>()

data.forEach { sweet ->
    // Fill the entires list
}
// Set the entries, decorate the pie  
}

Then in the xml:

<com.github.mikephil.charting.charts.PieChart
            android:id="@+id/consumed_data_sweets_rating_chart"
            android:layout_width="match_parent"
            android:layout_height="@dimen/chart_height"
            android:layout_gravity="center"
android:layout_margin="@dimen/material_component_cards_top_and_bottom_padding"
            android:setPieData="@{viewmodel.pieData}"

Where viewmodel.pieData is LiveData<List<SomeModel>> models in passed in ViewModel class.

So basically you have to reate custom data binding adapters for any properties of Charts you want to set.

Link to my project, look for ChartBindingAdapters class to see my custom adapters.

Deividas Strioga
  • 1,467
  • 16
  • 24
0

Yes, it is possible.

layout -tag must in xml file.

<layout >
        <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <com.github.mikephil.charting.charts.LineChart
                android:id="@+id/lineChart"
                android:layout_width="70dp"
                android:layout_height="60dp"
                android:orientation="vertical">
    <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ok"
            android:id="@+id/button1"
            android:background="#0058b6" />


    </LinearLayout>

</layout>

In Activity,

ActivityMainBinding binding; // Declaration

binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

binding.lineChart.setVisibility(Visibility.VISIBLE);
binding.button1.setVisibility(Visibility.VISIBLE);

binding.button1.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
        // Perform action on click
    }
});
Gayathri
  • 249
  • 3
  • 13