리오
08/05/2019, 11:53 PMval loginFieldsValid = MediatorLiveData<Boolean>()
val username = MutableLiveData<String>()
val password = MutableLiveData<String>()
val loginButtonEnabled = MutableLiveData<Boolean>()
And inside init
, I add the sources to the MediatorLiveData.
loginFieldsValid.addSource(username) { validateLoginFields() }
loginFieldsValid.addSource(password) { validateLoginFields() }
The validateLoginFields()
simply checks, if at the time both username and password fields are not null or empty string.
private fun validateLoginFields() {
loginButtonEnabled.value = !username.value.isNullOrEmpty() && !password.value.isNullOrEmpty()
}
The xml is still the same
<Button
android:id="@+id/login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:enabled="@{loginViewModel.loginButtonEnabled}"
android:onClick="@{() -> loginViewModel.onLogin()}"
android:text="@string/login"
app:layout_constraintEnd_toEndOf="@+id/password_text_input_layout"
app:layout_constraintTop_toBottomOf="@+id/password_text_input_layout" />
My issue is that the login button is always disabled even if I input some data into both fields.