Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Usage of ObservableField<String> over String in Android Databinding

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?

like image 794
jrhamza Avatar asked Jan 28 '26 16:01

jrhamza


1 Answers

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);
like image 142
Stanley Ko Avatar answered Jan 30 '26 20:01

Stanley Ko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!