https://kotlinlang.org logo
#android
Title
# android
u

리오

08/05/2019, 9:45 PM
I am trying to disable a login button if username edittext is empty. In LoginViewModel, there is a live data for username
val username = MutableLiveData<String>()
. In the xml, the edit text is defined as:
Copy code
<com.google.android.material.textfield.TextInputEditText
                android:id="@+id/username"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:ems="10"
                android:hint="@string/username_or_email"
                android:inputType="textPersonName"
                android:text="@={loginViewModel.username}" />
The login button is defined as:
Copy code
<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" />
The
loginButtonEnabled
is a variable in the LoginViewModel.
Copy code
val loginButtonEnabled = Transformations.map(username) {
        null != it
    }
a

Aslam Hossin

08/06/2019, 7:00 AM
You can use RxBinding for form validation. For getting started you can see this REPO https://github.com/mdaslamHossin/LoginForm-Validation
51 Views