Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Data Binding how to get user entered text and get it in code

I have a simple logIn form where user can enter username and password and hit button to LogIn.

I have my layout file, something like this activit_main.xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools">
    <data>
        <variable
            name="user"
            type="User"></variable>
<variable
            name="logInResponse"
            type="LogInResponse"></variable>
    </data>
<LinearLayout
            android:id="@+id/login_section_credentialsBody"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:inputType="text"
                android:hint="@{user.username}" />

            <EditText
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="@{user.password}" />

           <Button
                android:id="@+id/login"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Log In"
                android:onClick="callLogIn"/>


        </LinearLayout>
</Layout>

And Here is my MainActivity.java:

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

    public void callLogIn(View view) {
            ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
    textView.setText("Connecting...");
            loginInService.checkLogin(binding.getUser());
        }

The problem is first time it shows Log In and Password in the edit text as I created it and bind it in OnCreate, but when user enters the actual username and password then it is not taking the new values entered by user.

So how to get entered text by user?

like image 733
minal sharma Avatar asked Aug 28 '15 19:08

minal sharma


People also ask

How can I get text from EditText using DataBinding Android?

Currently, Android Data Binding is one way only. So when you set android:text="@{user. username}" that sets the text of that EditText to the user's current username. If the user or the username are Observable, then you will also get updates if they change.

What is DataBindingUtil in Android?

DataBindingUtil.setContentView(this, R.layout.activity_main) DataBindingUtil is a Utility class to create ViewDataBinding from layouts. A binding class is generated for each layout file. The default naming convention used for the generated class is based on the name of the layout file.

How do I get text in EditText?

Now, before we start we need to know some method of EditText which we use to get or fetch the text written in an EditText that is . getText() method and the other one is . setText() method that we use to set some pre-written text or string. EditText demo; demo=(EditText)findViewById(R.


4 Answers

According to George and Yigit's Google I/O 2016 data binding talk, you can do:

android:text="@={User.LogIn}

In your xml and the two way will work. The thing to note is the additional equals sign.

like image 76
GinsengRoses Avatar answered Oct 26 '22 21:10

GinsengRoses


You will need to have function in your User(viewmodel) where you can pass field data. For example below is my layout file code for login activity.

<EditText android:layout_width="0dp"
              android:id="@+id/email_address_input"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toTopOf="parent"
              android:hint="Email address"
              android:gravity="center"
              android:layout_height="wrap_content"/>

    <EditText android:layout_width="0dp"
              android:id="@+id/password_input"
              app:layout_constraintLeft_toLeftOf="parent"
              app:layout_constraintRight_toRightOf="parent"
              app:layout_constraintTop_toBottomOf="@id/email_address_input"
              android:hint="Password"
              android:gravity="center"
              android:layout_height="wrap_content"/>

    <Button android:layout_width="wrap_content"
            android:id="@+id/submit"
            android:text="Submit"
            android:onClick="@{()->viewModel.onClickEmailSignin(emailAddressInput.getText().toString(),passwordInput.getText().toString())}"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toBottomOf="@id/password_input"
            android:layout_height="wrap_content"/>

And Here is my viewmodel have function onClickEmailSignin()

fun onClickEmailSignin(email: String, password: String) {
   Log.d(LOG_TAG, "email $email password : $password");
}
like image 45
black_spark Avatar answered Oct 26 '22 21:10

black_spark


Currently android data binding is one way communication so it does not allow to get edittext current text as what you are expecting.

i had the same issue and after reading official google documentation i got this :

MyLayoutBinding binding=DataBindingUtil.setContentView(this,R.layout.my_layout);

now use this binding object to get reference to your views. Suppose you have edittext as

 <EditText
            android:id="@+id/edittext_chat_message"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:hint="@string/hint_typing"
            android:layout_marginRight="@dimen/margin_average"
            style="@style/regular_editTexts"/>

then get reference to edittext by binding.edittextChatMessage.getText().toString

no wonder data binding itself created edittext reference for you using its id name also this way of getting reference is very fast as compared to findViewById method.

like image 5
saksham Avatar answered Oct 26 '22 21:10

saksham


You can't.

Currently, Android Data Binding is one way only.

So when you set android:text="@{user.username}" that sets the text of that EditText to the user's current username. If the user or the username are Observable, then you will also get updates if they change.

However, it's one way only — the EditText will show the updates to the view model, but the view model won't get the changes to the EditText.

If you want those changes, you have to do it manually.

There's two simple ways here, and it will depend on your use case. One, set a TextWatcher and keep the name updated.

The other one, which I think is the one you'll need here, is to manually update it in the callLogIn method.

Also, see https://medium.com/@fabioCollini/android-data-binding-f9f9d3afc761 It's a little more complicated, but it might fit your use case.

like image 1
Nacho Avatar answered Oct 26 '22 21:10

Nacho