I am new to Android Databinding and have got one doubt on the usage of ObservableField. Suppose,
private ObservableField<String> name;
private String name;
In both cases, we have to call notifyPropertyChanged() while setting the new value. So, is there advantage of ObservableField over String?
ObservableField or LiveData is important concept of MVVM.
In your xml, you can define Observable data for xml. This data is always observed by xml. That means, if you set data to your name class, xml will be updated automatically.
<data>
<variable
name="name"
type="your.class.name"/>
</data>
And, use it in xml. For example, use name for TextView.
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@={name}"/>
Next, in your java code, insert your ObservableField name to binding.
YourBinding binding = DataBindingUtil.inflate(LayoutInflater.from(this), R.layout.your, null, false);
binding.setName(name);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With