event handling – Android data binding – unable to invoke the click handler-ThrowExceptions

Exception or error:

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

  1. the text box or
  2. 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}"/>
How to solve:

Can you try the following for your button?

<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}"/>

I get an error on the IDE ('!=', '%'... expected, got ':') but it works when the app is running… The important part is the “::”.

Hope this helps you!

###

As I known,the onClick event seems not work in fragment and I don’t why.Try use BindingAdapter annotation to define custom setter.

Leave a Reply

Your email address will not be published. Required fields are marked *