Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to compare strings in xml file using databinding

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.

like image 449
dzikovskyy Avatar asked Oct 14 '16 17:10

dzikovskyy


People also ask

How do I compare strings in Data Binding?

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.

Which is better Viewbinding and Data Binding?

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.

Can I use both Data Binding and Viewbinding?

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.

Which is the correct way to reference bound data in the XML layout?

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.


1 Answers

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"/>
like image 119
George Mount Avatar answered Dec 30 '22 05:12

George Mount