So far, the button is always disabled no matter wh...
# android
u
So far, the button is always disabled no matter what the username edittext value is. Should I be using a onchanged listener instead?
w
perhaps username is not null, but is empty?
u
!
w
Did you try
!it.isNullOrEmpty()
as condition in the transformation?
💯 1
☝️ 2
u
That was it
m
You should use ObservableField from data binding class instead of MutableLiveData. Unless you want to use custom class that extend Observable for your
username
props ref Observable[PrimitiveType]: https://developer.android.com/topic/libraries/data-binding/observability ref custom observable: https://android.jlelse.eu/android-data-binding-observables-part-4-da862a0f402d @리오
👎 2
g
You should use ObservableField from data binding class instead of MutableLiveData
Why so?
m
So you don't have to to observe in the fragment. Because ObservableField will observe data change on the property without hassle. *If I get @리오 question correctly. cmiiw
cmiiw @gildor
*unless @리오 wants to implement no logic in the UI at all even a little one my provided solutions might be ugly
Copy code
// viewmodel.kt
val username = ObservableFieldString("")

// fragment.kt
// do nothing, or process viewmodel.username.get()


// login.xml
<variable name="vm" />

<EditText
android:text="@={vm.username}"
/>
<LoginButton
android:disabled="@{TextUtils.isEmpty(vm.username)}"
>
*I forgot a few syntax above, please change accordingly. Use
multi-way e.g (equals sign after @)
databinding if I recall correctly, so you don't have to listen for
username
change.
g
@miqbaldc MutableLiveData is completely fine and more safe replacement for ObservableField and supported by bindings and one day ObservableField even may be replaced by LiveData
m
I see. That's an awesome and new info for me. *been dangling with java and building an
android library
instead of
android application
. 😂 Thanks @gildor bookmarked.