In my android app:
dataBinding {
enabled = true
}
in my activity:
class RegistrationActivity : RootActivity() {
private lateinit var viewBinding: RegistrationActivityBinding // use "registration_activity.xml"
private lateinit var registrationViewModel: RegistrationViewModel
private var wallet = Wallet()
companion object {
private val TAG = RegistrationActivity::class.java.name
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Debug.d(TAG, "onCreate: savedInstanceState = $savedInstanceState")
viewBinding = RegistrationActivityBinding.inflate(layoutInflater)
setContentView(viewBinding.root)
viewBinding.setVariable(BR.model, wallet)
viewBinding.executePendingBindings()
init()
}
private fun init() {
viewBinding.buttonRegistration.setOnClickListener {
Debug.d(TAG, "initLogic: click_button, wallet = $wallet")
}
}
}
here my model:
class Wallet() : Serializable {
var email: String? = null
var password: String? = null
override fun toString(): String {
return "\nWallet(email = $email, password = $password)"
}
}
in xml:
<data>
<variable
name="model"
type="com.myproject.model.Wallet" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/common_color_bg">
<include
android:id="@+id/registrationToolbar"
layout="@layout/tool_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:title="@{@string/registration}" />
<EditText
android:id="@+id/editTextEmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/default_margin"
android:hint="@string/email"
android:importantForAutofill="no"
android:inputType="textEmailAddress"
android:text="@{model.email}"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/registrationToolbar" />
<EditText
android:id="@+id/editTextPassword"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:hint="@string/password"
android:importantForAutofill="no"
android:inputType="textPassword"
android:text="@{model.password}"
app:layout_constraintEnd_toEndOf="@+id/editTextEmail"
app:layout_constraintStart_toStartOf="@+id/editTextEmail"
app:layout_constraintTop_toBottomOf="@+id/editTextEmail" />
<EditText
android:id="@+id/editTextPasswordRetype"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="@dimen/default_margin"
android:hint="@string/retypePassword"
android:importantForAutofill="no"
android:inputType="textPassword"
app:layout_constraintEnd_toEndOf="@+id/editTextEmail"
app:layout_constraintStart_toStartOf="@+id/editTextEmail"
app:layout_constraintTop_toBottomOf="@+id/editTextPassword" />
<com.google.android.material.button.MaterialButton
android:id="@+id/buttonRegistration"
android:layout_width="0dp"
android:layout_height="@dimen/button_height"
android:layout_margin="@dimen/default_margin"
android:layout_marginBottom="@dimen/default_margin"
android:text="@string/registration"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/editTextPasswordRetype" />
<include
android:id="@+id/progressBarLayout"
layout="@layout/progress_bar_layout"
android:visibility="gone" />
</androidx.constraintlayout.widget.ConstraintLayout>
I input some email in editTextEmail and click button. As result call method:
viewBinding.buttonRegistration.setOnClickListener
Nice.
But here log:
click_button, wallet = Wallet(email = null, password = null)
As you can see the email is null
But I need pass email from xml to activity. I use viewbinding. Why email not pass to activity via this:
android:text="@{model.email}"
?
You have to use two way binding to back data from View
to ViewModel
. Use @= to enable two way data binding in your xml
like:
android:text="@={model.email}"