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

Paul Dhaliwal

10/09/2019, 4:15 PM
Is there a Kotlin language feature that could reduce the verbosity of accessing views from our classes with Data Binding? According to this (https://android-review.googlesource.com/c/platform/frameworks/support/+/882241) kotlinx.android.synthetic is no longer recommended. And since I use the data binding library already I figured I’d remove all those imports and just use data binding to access the views. However I end up with a lot of calls such as
dataBinding.featureHostBottomNavigationViewFrame.featureHostCustomerAccountPointBalance.text = someNumber
. I don’t know too much about Kotlin yet however I’m curious if there’s some way I can simplify these calls to just
featureHostCustomerAccountPointBalance.text = someNumber
m

Marek Osvald

10/09/2019, 4:40 PM
We're storing pointers exposed in the generated
Binding
class as private fields in `Fragment`s.
w

wasyl

10/09/2019, 4:52 PM
If you have many calls you can wrap them in
with(dataBinding) { ... }
. But also data binding exposes even nested views:
binding.nested
works even if
nested
is child of child of child … of the root
kotlinx.android.synthetic is no longer recommended
Last I saw discussion about this it turned out that
androidx.synthetic
isn’t “not recommended” overall, it’s just not recommended in the aosp repo
p

Paul Dhaliwal

10/09/2019, 5:07 PM
Do you mean that by calling binding.nested I should from there have access to all views in the layout? I’m not seeing that field in under binding. That would be great as I could just save that value as ” val layout” for example and access all views in the layout from there, if I’m understanding correctly.
w

wasyl

10/09/2019, 5:11 PM
I mean that the binding class should have top-level fields for all views in the layout, even nested ones. So it’s enough to store
binding
in the field, and then you should be able to call
binding.topLevelView
,
binding.someChild
,
binding.someVeryDeeplyNestedChildView
p

Paul Dhaliwal

10/09/2019, 5:13 PM
Ah I see. No that doesn’t work for me. I don’t see the child views when using binding, only the top level views. So I have to go binding.parentview.childview and not binding.childview. Is it possible I misconfigured something?
w

wasyl

10/09/2019, 5:16 PM
No idea 😕 Perhaps you’re using
<layout>
tags? Or some older version of data binding?