I have searched for some similar questions before posting - however I have a general question when it comes to Android and data binding (and the other answers I check did no
I realize this is some years later but faced with the same issues I ran across the following which may help lessen the load.
RoboBinding - handles binding - as mentioned above RoboGuice - does dependency injection
There is a very nice video presentation of RoboBinding that will help explain what and how.
I am not affiliated with either effort but they do look promising for those folks still trying to resolve the binding issues, especially on involved code. RoboBinding also handles bidirectional updates.
Since you first asked your question, the landscape has changed a lot.
Most importantly Stuart Lodge gave us MVVMCross
.
This project provides a cross-platform mvvm mobile development framework built on top of Silverlight for WP7, Mono for Android and MonoTouch for iOS, and the WinRT XAML framework for Windows 8 Store applications This project makes extensive use of Portable Class Libraries to provide maintainable cross platform C# native applications.
It provides data binding in your Views
against ViewModels
For example, it enables the following:
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
local:MvxBind="{'Text':{'Path':'Query','Mode':'TwoWay'}}" />
Resources:
Github Page: https://github.com/slodge/MvvmCross
Presentation: http://slodge.blogspot.co.uk/2012/12/mvvmcross-video-presentation-xaminar.html
And a very good introductional tutorial: Building Android Apps with MVVM and Data Binding
In addition to Oleksii's answer, for those who want to see a sample project (it seems Google hasn't provided any sample project yet), I just made one and pushed it to GitHub.
A few notes:
classpath "com.android.databinding:dataBinder:1.0-rc0"
doesn't work for me so I use classpath group: 'com.android.databinding', name: 'dataBinder', version: '1.0-rc0'
android.databinding.Observable
or extends BaseObservable
if you need the ability to notify when data changes.Well, my Android-Binding project is trying to do data-binding via XML layout. Because there's no build-in method provided by Google (and I can't foresee that Google will do so), that's the reason I started this project.
Reading a bit about this topic, I just found RoboBinding, a "data-binding Presentation Model framework" for Android.
Similar to the Android-binding project, you can bind properties (one-way or two-way) and events to your views in XML using an extra namespace.
Although it is no built-in approach either, it might be a great help for you.
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);
}