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

리오

08/05/2019, 9:46 PM
So far, the button is always disabled no matter what the username edittext value is. Should I be using a onchanged listener instead?
w

wasyl

08/05/2019, 9:47 PM
perhaps username is not null, but is empty?
u

리오

08/05/2019, 9:47 PM
!
w

wasyl

08/05/2019, 9:47 PM
Did you try
!it.isNullOrEmpty()
as condition in the transformation?
💯 1
☝️ 2
u

리오

08/05/2019, 9:48 PM
That was it
m

miqbaldc

08/06/2019, 3:14 AM
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

gildor

08/06/2019, 3:16 AM
You should use ObservableField from data binding class instead of MutableLiveData
Why so?
m

miqbaldc

08/06/2019, 3:17 AM
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

gildor

08/06/2019, 3:37 AM
@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

miqbaldc

08/06/2019, 3:40 AM
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.
4 Views