Last week, I was talking to someone on my team and it became apparent that they weren’t aware of one super useful feature of data binding. If you know me at all, you know that I like love this library, and I would take every opportunity to spread the love around.
Let’s take a very simple example – say we have a CheckBox
and a TextView
in a layout file, and we want the TextView
to display if the CheckBox
is checked or not. I made this adorable gif to illustrate:
Cheeky!</p>
The corresponding layout file is pretty straightforward:
```xml
```
Let us first define our string resource:
```xml
Is it checked? %1$s
```
where `%1$s` would be the checked state of the `CheckBox`.
All that is left is to use this string in our layout file! We can use two data binding features: [String formatting](https://developer.android.com/topic/libraries/data-binding/expressions#resources), and ID referencing.
```xml
android:text="@{@string/is_it_checked(cheekyCheckbox.checked)}"
```
Yes, my friend -- we can reference other views in our data binding expressions via their ID! **Note that data binding converts IdsToCamelCase, so be sure to follow the convention.**
Happy binding!