0

In my source code I have the following ButterKnife annotation:

public class MyActivity extends AppCompatActivity {

    @BindString(R2.string.settings_progress)
    String progressText;
    .
    .
    .
}  

Now I've decided to switch to Android Data Binding.

What would the equivalent be if I use Android data binding?

Should I declare a variable inside <data></data> tags in the activities XML file?

Henrik
  • 1,983
  • 3
  • 28
  • 52
  • see: https://zarah.dev/2016/07/19/using-resource-ids-in-data-binding.html and https://stackoverflow.com/questions/32273517/databinding-how-to-get-resource-by-dynamic-id, might help you – Anand Jun 03 '21 at 02:53

1 Answers1

0

You need to use the Data Binding Library

The basic usage is like this:

findViewById<TextView>(R.id.sample_text).apply {
    text = viewModel.userName
}

Or in XML

<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="viewmodel"
            type="com.myapp.data.ViewModel" />
    </data>
    <TextView
        android:text="@{viewmodel.userName}" />
</layout>

Both of these approaches require using a ViewModel to connect the binding. However, I believe with the first approach you can bind to any observable property, (not 100% sure about the details on that).

RestingRobot
  • 2,938
  • 1
  • 23
  • 36