Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

String comparison on Android Data Binding

I'm trying to make a string comparison with android XML data binding, but I'm not having the right results.

Evaluating my expression in code, I try notice.action == "continue" and this is false. And in data binding, this is false too, of course.

android:textColor='@{ notice.action == "continue" ? @color/enabledPurple : @color/disabledGray}' 

It only gets true when I do notice.action.equals("continue") by code. This is the intended behavior. My problem is that I can't accomplish this with data binding expressions, because it won't run methods like equals. What can I do to replace the comparison expression with another one that works?

I'm using this guide.

Edit: I was wrong, methods are allowed in XML. Did it this way:

android:textColor='@{ notice.action.equals("continue") ? @color/enabledPurple : @color/disabledGray}' 
like image 219
Ricardo Melo Avatar asked Mar 14 '16 06:03

Ricardo Melo


People also ask

How do I compare strings in databinding?

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.

How do I check if a string is equal in Android?

In order to compare two strings, we have to use a method called “equals”. Type the following into the parentheses of your If Statement: car1. equals() . In the parentheses of THIS code, write car2 as a parameter.

Is Data Binding good in Android?

Android Jetpack is a set of libraries designed to help developers follow best practices and create code quickly and simply. The Data Binding Library is one of them. Data Binding allows you to effortlessly communicate across views and data sources.

What is 2 way data binding in Android?

The @={} notation, which importantly includes the "=" sign, receives data changes to the property and listen to user updates at the same time. // Avoids infinite loops.


1 Answers

It can be do in two way :-

1. First way inside xml :-

    android:textColor="@{notice.action.equals(`continue`) ? @color/enabledPurple : @color/disabledGray }" 

2. Second way (programatically) Inside xml :-

app:setColor="@{notice.action}"  inside activity or custom class : -         @BindingAdapter("setColor")         public static void setTextColor(TextView textView, String s) {               Context context = textView.getContext();          textView.setTextColor(s.equals("continue") ? context.getResources().getColor(R.color.enabledPurple) : context.getResources().getColor(R.color.disabledGray));         } 
like image 160
Sagar Chorage Avatar answered Sep 30 '22 16:09

Sagar Chorage