I have created a simple login screen using the databinding as described at http://developer.android.com/tools/data-binding/guide.html however I am unable to get the notification of text changed from
- the text box or
- the button click.
I think for the text box notification, the android team might not have implemented it completely. However, I fail to understand my mistake for the button click handler.
The fragment code looks like
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
FragmentAccountSetupInitialBinding binding = DataBindingUtil.inflate(inflater, R.layout.fragment_account_setup_initial, container, false);
View view = binding.getRoot();
binding.setAccount(new UserAccount());
return view;
}
The ViewModel for user account is as defined below
public class UserAccount extends BaseObservable {
private String _email;
private String _password;
@Bindable
public String getEmail() {
return _email;
}
public void setEmail(String email) {
if(!TextUtils.equals(_email, email)) {
_email= email;
notifyPropertyChanged(BR.email);
}
}
@Bindable
public String getPassword() {
return _password;
}
public void setPassword(String password) {
if(!TextUtils.equals(_password, password)) {
_password = password;
notifyPropertyChanged(BR.password);
}
}
public void onSignInButtonClick(View view) {
// Sign in now
}
}
And the fragment layout is
<layout
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable name="account" type="project.namespace.UserAccount"/>
</data>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="56dp"
android:paddingLeft="24dp"
android:paddingRight="24dp">
<!-- Email Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/email"
android:text="@{account.email}"/>
</android.support.design.widget.TextInputLayout>
<!-- Password Label -->
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp">
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/password"
android:text="@{account.password}"/>
</android.support.design.widget.TextInputLayout>
<android.support.v7.widget.AppCompatButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginBottom="24dp"
android:padding="12dp"
android:text="@string/signin"
android:onClick="@{account.onSignInButtonClick}"/>
</LinearLayout>
</FrameLayout>
</layout>
Update
Moving to android studio 2.1 beta 2 solves the first problem. Updated the layout as follows
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:hint="@string/email"
android:text="@={account.email}"/>
one way
only, currently. So only changes in your model notify the view - not the other way round. This is a feature coming withAndroid Studio 2.1
(Source. Your code looks fine, could you explain your problem more? Does youronClick
method get called?onClick
is not getting calledonClick