Search code examples
androidandroid-databinding

Android Data Binding : Using the @={} syntax for two data binding causes the Binding file not to be generated


I am trying to use the 2 way data binding feature of the android 2 way data binding library .

My gradle version is

dependencies {
    classpath 'com.android.tools.build:gradle:2.2.1'
} 

When I use the library for simple binding it works

 <EditText
            android:id="@+id/text_view_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:hint="Enter Username"
            android:inputType="text"
            android:text="@{model.username}"
        />

When I use the @={} format for the 2 way data binding it doesnt work

 <EditText
            android:id="@+id/text_view_username"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginBottom="20dp"
            android:gravity="center"
            android:hint="Enter Username"
            android:inputType="text"
            android:text="@={model.username}"
        />

It doesnt work . I get the following error

Error:(9, 31) error: package databinding does not exist

Basically the databinding classes arent getting generated . Please help In case any other information is required let me know .

public class Login implements IBaseModel {
    public String username, password;

    public Login(String username, String password) {
        this.username = username;
        this.password = password;
    }

    public String getUsername() {
        return this.username;
    }

    public String getPassword() {
        return this.password;
    }
}

and the view model class is

public class LoginViewModel implements IViewModel {
    private Subscription subscription;
    private ILoginDataListener dataListener;
    private Login login;

    Context context;


    String getUserName() {
        return this.login.getUsername();
    }

    LoginViewModel(Context ctx, Login login) {
        this.context = ctx;
        this.login = login;
    }

    public void onClickSubmit(View view) {
        Toast.makeText(context, "From View Model username is " + this.login.getUsername() + " the password " + this.login.getPassword(), Toast.LENGTH_SHORT).show();

        Toast.makeText(context, "From View Model", Toast.LENGTH_SHORT).show();

        loginRequest("Rajendra", "12345");
    }

    public void updateValue(View view) {
        this.login.password = "dummy";
        this.login.username = "simplefool";
    }

    @Override
    public void destroy() {

        if (subscription != null && !subscription.isUnsubscribed()) subscription.unsubscribe();
        subscription = null;
        context = null;
        dataListener = null;    
    }
}

Solution

  • From my comment:

    There should be more information in the log cat, not as pretty as normally, but there is more. Please post it, and format it. But I do think that the data binding is searching for a setUserName method (in your Login.class), which does not exist, throwing an error.

    The methods should also call notifyPropertyChanged(BR.propertyName), as you correctly mentioned in you comment.

    In your Login.class add

    public void setUsername(String username) { 
        this.username = username; 
        notifyPropertyChanged(BR.username); 
    } 
    
    public void setPassword(String password) { 
        this.password = password; 
        notifyPropertyChanged(BR.password); 
    }