Reputation: 3730
I have the standard login form which has 2 edit text (one for email and other for password) and a button for log-in.
I want to send email and password to the server when the user clicks on login button.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data class="ActivityMainBinding">
<variable
name="login"
type="com.example.itstym.reminder.loginViewModel" />
<variable
name="handler"
type="com.example.itstym.reminder.LoginHandler" />
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.itstym.reminder.MainActivity">
<EditText
app:error="@{login.errorEmail}"
android:text="@{login.userEmailAddress}"
android:hint="Email Address"
android:id="@+id/email_address"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="24dp"/>
<EditText
app:error="@{login.errorPassword}"
android:text="@{login.userPassword}"
android:hint="Password"
android:id="@+id/user_password"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="0dp"
android:ems="10"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="@+id/email_address"
android:layout_marginTop="16dp"
app:layout_constraintTop_toBottomOf="@+id/email_address"/>
<Button
android:onClick="@{() -> handler.onLoginButtonClicked(login)}"
android:id="@+id/submit_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginStart="16dp"
android:layout_marginTop="24dp"
android:text="Sign In"
app:layout_constraintEnd_toEndOf="@+id/user_password"
app:layout_constraintStart_toStartOf="@+id/user_password"
app:layout_constraintTop_toBottomOf="@+id/user_password"/>
</android.support.constraint.ConstraintLayout>
</layout>
MainActivity.kt
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding=DataBindingUtil.setContentView<com.example.itstym.reminder.databinding.ActivityMainBinding>(this@MainActivity,R.layout.activity_main)
Log.i("Binding class ",binding.javaClass.simpleName.toString())
Log.i("type ",binding.javaClass.toString())
val login:loginViewModel=loginViewModel()
binding.login=login
val handler= LoginHandler()
binding.handler=handler
}
}
loginViewModel.kt
class loginViewModel(): BaseObservable() {
@Bindable
var userEmailAddress:String= String()
set(userEmailAddress){
field=userEmailAddress
notifyPropertyChanged(R.id.email_address)
/*to check Email for validation on every character inserted by user*/
notifyPropertyChanged(BR.errorEmail)
}
get() {
return field
}
@Bindable
var userPassword:String = String()
set(userPassword){
field=userPassword
notifyPropertyChanged(R.id.user_password)
notifyPropertyChanged(BR.errorPassword)
}
get() {
return field
}
}
//some code removed for readablitly purpose
LoginHandler.kt
class LoginHandler() {
fun onLoginButtonClicked(userInfo: loginViewModel){
Log.i("Button Clicked ","yup")
Log.i("Email is ",userInfo.userEmailAddress)
Log.i("Password is ",userInfo.userPassword)
}
}
Problem: I am not able to print the email address and password inside the written in onLoginButtonClicked(). Why??
Any lead will helpfull.
Before downvoting the question, add the comment that will helps in improving the question.
Upvotes: 1
Views: 1754
Reputation: 1952
In your activity_main.xml file, you're only using One-Way Data Binding. If you want to use Two-Way Data Binding, you must use the equal sign after the at sign (@=) for example:
One-way Data Binding (Only update the view when the property change)
android:text="@{login.userEmailAddress}"
android:text="@{login.userPassword}"
Two-way Data Binding (Update the view when the property change and vice-versa)
android:text="@={login.userEmailAddress}"
android:text="@={login.userPassword}"
There is a great talk about Data Binding by Kevin Pelgrim if you want to go deeper with the topic.
Extra Resources:
Episode 057: Data Binding with GDE Lisa Wray - Fragmented Podcast
Episode 35: Data Bound - Android Developer Backstage
Hope it helps!
Upvotes: 3