How can I compare my object String field value to another String value in xml file using databinding? Is it possible to do so in xml file or should I create a method somewhere in my project with @BindingAdapter
annotation?
Below is what I've tried so far and it didn't worked. It would also be good to compare with String resource value and not with hardcoded string value.
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase("male")}"
android:text="@string/male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase("female")}"
android:text="@string/female"/>
</RadioGroup>
Thanks for help.
There is no need to import the String class into your layout file. To check whether two strings have equal value or not equals() method should be used. = is used to check the whether the two strings refer to the same reference object or not.
Note: In many cases, view binding can provide the same benefits as data binding with simpler implementation and better performance. If you are using data binding primarily to replace findViewById() calls, consider using view binding instead.
View binding doesn't support layout variables or layout expressions, so it can't be used to declare dynamic UI content straight from XML layout files. View binding doesn't support two-way data binding.
Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression. Expressions can be used for many purposes depending on your requirement.
You have it almost correct. String constants can't use double-quotes within double-quotes in XML, so android data binding supports using back-quote in the expression:
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(`male`)}"
android:text="@string/male"/>
That allows you to mix character constants with single-quotes along with string constants.
XML also allows the use of single quotes for attribute values, so you can use double quotes within the expression. This is the more common approach:
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked='@{user.gender.equalsIgnoreCase("female")}'
android:text="@string/female"/>
You can skip the whole thing and use string resources or constants:
<RadioButton
android:id="@+id/male"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(@string/male)}"
android:text="@string/male"/>
<RadioButton
android:id="@+id/female"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:checked="@{user.gender.equalsIgnoreCase(StringConstants.FEMALE)}"
android:text="@string/female"/>
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