curioustechizen curioustechizen - 1 year ago 133
Android Question

Is it possible to bind one ObservableField to another?

I understand that the purpose of Android's data-binding library is for views to observe data and automatically update when that data changes.

Question: Is it possible for data to observe other data? For example, can I have one

"depend on" or "bind to" the value of another or a set of other
s? Currently, I have implemented this manually - every time any of the "dependee"
s change, I compute the dependent field and update its value.


My use-case is I want all "logic" to be outside the View - so I want to put all my logic in the "data" class (
, if I may). I have a button whose state I want to set to enabled/disabled depending on the contents of several other fields. This example illustrates what I have in mind.

My layout file looks like this

<variable name="register" class="com.example.RegisterViewModel"/>
<EditText android:id="@+id/edPassword" />
<EditText android:id="@+id/edConfirm" />
<Button android:text="Register" android:enabled="@{register.isValid}" />

And, my View code is as follows:

class RegisterView extends View {
protected void onFinishInflate() {
RegisterViewBinding binding = DataBindingUtil.bind(this);
RegisterViewModel register = new RegisterViewModel();
binding.edPassword.setOnFocusChangeListener(new OnFocusChangeListener(){
@Override public void onFocusChange(View v, boolean hasFocus){

//Similarly for other fields.

Here is my ViewModel

class RegisterViewModel {
public final ObservableField<String> username = new ObservableField<>();
private final ObservableField<String> password = new ObservableField<>();
private final ObservableField<String> confirmPassword = new ObservableField<>();
public final ObservableBoolean isValid;

//Dependee Observables - isValid depends on all of these
private final ObservableBoolean isUsernameValid = new ObservableBoolean();
private final ObservableBoolean isPasswordValid = new ObservableBoolean();
private final ObservableBoolean arePasswordsSame = new ObservableBoolean();

public RegisterViewModel(){
//Can this binding be made observable so that isValid automatically
//updates whenever isUsernameValid/isPasswordValid/arePasswordsSame change?
isValid = new ObservableBoolean(isUsernameValid.get() &&
isPasswordValid.get() &&


public void updateUsername(String name) {

public void updatePassword(String pwd) {

public void updateConfirmPassword(String cnf) {

//Looking to avoid this altogether
private void updateDependents() {
isValid.set(isUsernameValid.get() &&
isPasswordValid.get() &&

Answer Source

It is not possible to data bind two ObservableFields using binding syntax in Android data binding. However, you can bind them with code:

class RegisterViewModel {
    public final ObservableField<String> username = new ObservableField<>();
    public final ObservableField<String> password = new ObservableField<>();
    public final ObservableField<String> confirmPassword = new ObservableField<>();
    public final ObservableBoolean isValid = new ObservableBoolean();

    private boolean isUsernameValid;
    private boolean isPasswordValid;
    private boolean arePasswordsSame;

    public RegisterViewModel() {
        // You can use 3 different callbacks, but I'll use just one here
        // with 'if' statements -- it will save allocating 2 Object.
        OnPropertyChangedCallback callback = new OnPropertyChangedCallback() {
            public void onPropertyChanged(Observable sender, int propertyId) {
                if (sender == username) {
                    isUsernameValid = ValidationUtils.validateUsername(name);
                } else if (sender == password) {
                    isPasswordValid = ValidationUtils.validatePassword(pwd);
                } else if (sender == confirmPassword) {
                    arePasswordsSame = password.get()
                } else {
                    // shouldn't get here...
                isValid.set(isUsernameValid && isPasswordValid && arePasswordsSame);


Here, I've assumed that empty username, password, and confirmPassword are invalid. Seemed a safe assumption.

I don't see a tremendous need for private ObservableFields. ObservableField was designed to be bound to by the UI and if you can't, you can use other data types. If you find them useful for internal binding using callbacks like the above, then go for it.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download