6

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}"/>
5
  • go through this link onClick Handler you can find solution for this question. Commented Apr 18, 2016 at 5:14
  • I think that is exactly what I am doing. Instead of a separate MyHandler class I am handling it in the UserAccount class.
    – resp78
    Commented Apr 18, 2016 at 5:41
  • 1
    Data Binding is one way only, currently. So only changes in your model notify the view - not the other way round. This is a feature coming with Android Studio 2.1(Source. Your code looks fine, could you explain your problem more? Does your onClick method get called?
    – yennsarah
    Commented Apr 18, 2016 at 5:49
  • Thanks for the two-way binding update. If the official docs would have mentioned it would have saved me some time. Thanks for the 2.1 update. No onClick is not getting called
    – resp78
    Commented Apr 18, 2016 at 6:05
  • I have updated to android studio 2.1 and the setters are working but still no luck with the onClick
    – resp78
    Commented Apr 18, 2016 at 8:01

2 Answers 2

2

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!

1

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

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.