guys I have login page with 2 editText field (username, password) and with 1 button (Submit)
I want to enable Submit button when something is written is both editText.
I created two LiveData variable
val _userName = MutableLiveData<String>()
val userName: LiveData<String>
get() = _userName
val _password = MutableLiveData<String>()
val password: LiveData<String>
get() = _password
this is my submit button in xml
<com.google.android.material.button.MaterialButton
android:id="@ id/uSubmit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="80dp"
android:text="@string/log_in_button_text"
android:enabled="@{authViewModel.isSubmitButtonEnabled()}"
android:onClick="@{_ -> authViewModel.uUserLogin()}"
android:clickable="false"
app:layout_constraintEnd_toEndOf="@ id/passwordInputLayout"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="@ id/passwordInputLayout"
app:layout_constraintTop_toBottomOf="@ id/passwordInputLayout" />
I tried to wrote this code in viewModel to enable button when something is written in userName field
var isSubmitButtonEnabled = (Transformations.map(userName) {
it.isNotEmpty()
})
and this works fine. Now I want to enable button when in both userName and password variable is something written. How can I do that ?
CodePudding user response:
There is no need to use viewmodel
to achieve this. You can simply use only xml
databinding
to get it right.
<com.google.android.material.button.MaterialButton
android:id="@ id/uSubmit"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="@string/log_in_button_text"
android:enabled="@{!PASSWORD_EDITTEXT_ID_IN_CAMEL_CASE.text.toString().isEmpty() && !USERNAME_EDITTEXT_ID_IN_CAMEL_CASE.text.toString().isEmpty()}"
... />