Search code examples
androidmodel-view-controllerandroid-viewandroid-viewbinderpresentation-model

Data binding: presentation model and view, 3rd party libs in Android


I'm quickly realizing this is going to be an issue in Android with a lot of boilerplate and as I started refactoring my code I'm now effectively writing my own half-@ssed version of data binding. I don't want to spend more time generalizing it and re-inventing the wheel. I was wondering if there are any good solutions out there as 3rd party libraries that the community uses.

I've found robo-bindings and I really liked their presentation (focus on unit testing their own stuff, robustness, etc) but it seems like they remain quite small and I'm worried about issues with their library and general support/evolution going forward.

Any other libraries people are using?

Thanks.


Solution

  • Android M will provide powerful library for data binding!

    It's available now in dev-preview version.

    It looks amazing inside xml and java files:

    <TextView android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@{user.firstName}"
        />
    

    Java bean:

    public class User {
       private final String firstName;
       private final String lastName;
       public User(String firstName, String lastName) {
           this.firstName = firstName;
           this.lastName = lastName;
       }
       public String getFirstName() {
           return this.firstName;
       }
       public String getLastName() {
           return this.lastName;
       }
    }
    

    Binding:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
       User user = new User("Test", "User");
       binding.setUser(user);
    }