I would suggest reconsidering your approach. One of the main benefits of data binding is the use of more expressive presentation code (in this case, XML). While there is a balance between how much work you really want to do in XML and in the presentation model, your case is a great example of too much work being done in the presentation model. In your code, these are not observable fields that depend on other fields, but view data that depends on data from other views. An observable field is just a representation of this data, and when possible, you should look for creating dependencies in the presentation layer, not in the data layer.
The approach I would like to propose is to start at the presentation level (XML) and assume that you do not have a holistic view model, but only data attached to the views. E.g. you can start with something like this:
<layout> <LinearLayout> <EditText android:text="@{username}"/> <EditText text="@{password}" /> <EditText text="@{confirmPassword}" /> <Button android:text="Register" android:enabled="@{password.equals(confirmPassword) && ...password validation...}" /> </LinearLayout> </layout>
After this first step, you will quickly realize that the password verification logic does not make sense here, since it is not trivial, so you should go:
<layout> <import "com.example.ValidationUtils"/> <LinearLayout> <EditText android:text="@{username}"/> <EditText text="@{password}" /> <EditText text="@{confirmPassword}" /> <Button android:text="Register" android:enabled="@{password.equals(confirmPassword) && ValidationUtils.validatePassword(password)}" /> </LinearLayout>
At this point, you just need a container for the username, password, and Password fields so you can pass them in, so you just add the viewModel variable.
<layout> <import "com.example.ValidationUtils"/> <variable name="viewModel" type="com.example.Register"/> <LinearLayout> <EditText android:text="@{viewModel.username}"/> <EditText text="@{viewModel.password}" /> <EditText text="@{viewModel.confirmPassword}" /> <Button android:text="Register" android:enabled="@{password.equals(confirmPassword) && ValidationUtils.validatePassword(password)}" /> </LinearLayout>
Notice how much nicer this is, and you didn't even need to see Java code.
PS: you could also replace the included expression with something like ValidationUtils.validateEntries(username, password, confirmPassword) if you want. This is a more stylish choice; this does not greatly affect the expressiveness of the view code, and anyone reading XML can figure out what the view is trying to achieve without looking in several places.