I'm trying to use DataBinding in Android. But i have a question in my mind.
I want to send edittext's text to binding functions inside xml. I mean; When the login button is clicked, I want to get the current username and password from the xml IDs. Is it possible?
Here what I want:
android:onClick="@{() -> login.onLogin(login_edt_username.text, login_edt_password.text)}"
My Current Usage:
Code:
@Override
protected void onStart() {
super.onStart();
binding= DataBindingUtil.setContentView(this,R.layout.activity_login);
}
@Override
public void onLogin() {
login(binding.loginEdtUserName.getText(),binding.loginEdtPassword.getText());
}
Xml:
<layout>
<data>
<variable
name="login"
type="interfaces.login.LoginInterface" />
</data>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_login"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.test.friends.LoginActivity">
<EditText
android:id="@+id/login_edt_user_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username" />
<EditText
android:id="@+id/login_edt_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/login_edt_user_name"
android:hint="Password"
android:inputType="textPassword" />
<Button
android:id="@+id/login_btn_sign_in"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> login.onLogin()}"
android:layout_below="@+id/login_edt_password"
android:text="SIGN IN" />
<Button
android:id="@+id/login_btn_register"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="@{() -> login.onRegister()}"
android:layout_below="@+id/login_btn_sign_in"
android:text="REGISTER" />
</RelativeLayout>
Thanks in advance.
If You want to use View Binding & Data Binding Together in a single layout you need to use only data binding because view binding is the subset of data binding data binding provide the functionality of view binding.
ViewBinding vs DataBinding The one and only function of View Binding is to bind the views in the code, while Data Binding offers some more options like Binding Expressions, which allows us to write expressions the connect variables to the views in the layout.
Layout Binding expressions Expressions in the XML layout files are assigned to a value of the attribute properties using the “ @{} " syntax. We just need to use the basic syntax @{} in an assignment expression.
This can be solved using 2-way Databinding (available since version 2.1 of the Gradle Plugin).
Alternative 1:
Import android.view.View
in your XML:
<data ...>
<import type="android.view.View"/>
</data>
Then, you will be able to reference some attributes of your Views directly in XML. In Lambda Expressions, like you're using, you can also use the Views like you would in Java. Concretely:
android:onClick="@{() -> login.onLogin(login_edt_username.getText().toString(), login_edt_password.getText().toString())}"
Alternative 2: Using a POJO. Given a POJO modelling your credential information, such as
public class Credentials {
public String username;
public String password;
}
, after declaring this model in your XML
<data>
<variable
name="login"
type="interfaces.login.LoginInterface" />
<variable
name="credentials"
type="models.login.Credentials" />
</data>
You could do:
<!-- .... -->
<EditText
android:id="@+id/login_edt_user_name"
android:layout_width="match_parent"
<!-- This binds the value of the Edittext to the Model. Note the equals sign! -->
android:text="@={credentials.username}" />
<EditText
android:id="@+id/login_edt_password"
<!-- Same here -->
android:text="@={credentials.password}" />
<!-- ... -->
and finally, to fit your requirement
android:onClick="@{() -> login.onLogin(credentials.username, credentials.password)}"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With