I'm new to Kotlin. Among other very interesting things, I've found the Android extensions which, according to the documentation, should let me use activities' views without the need of findViewById
.
Actually it works very well by adding just this line in my imports:
import kotlinx.android.synthetic.main.<layout>.*
The problem is when two different layouts contain a widget with the same id (i.e. the same name for the synthetic property),
e.g. two different TextView
s with the id txtTitle
. Say the first one is on an activity and the second one belongs to the layout used inside of an adapter.
When I try to call a method on the first TextView (the activity's one) I can't see the expected result, as if the call would be done on another view. As confirmation of this, when I call txtTitle.parent
, I see the parent and the siblings of the other txtTitle
rather than the ones expected.
Am I doing something wrong? The only ways I've found to bypass this issue is to use different names in all my layouts or continue to use findViewById
, but it would be a pity to waste this language feature...
Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.
In November 2020, we announced that this plugin has been deprecated in favor of better solutions, and we recommended removing the plugin from your projects. We know many developers still depend on this plugin's features, and we've extended the support timeframe so you have more time to complete your migrations.
The Kotlin Android Extensions is a compiler plugin that allows you to access views from your XML directly in activities, fragments and views using what they refer to as synthetic properties. To use it, ensure that the plugin is enabled in your module's build.gradle file: apply plugin: 'kotlin-android-extensions'
For every layout file, Kotlin Synthetics creates an autogenerated class containing your view— as simple as that. You just have to import this plugin in your Gradle file, and you are all set to directly refer to the view reference variables. It calls findViewById internally only once and then caches it.
The kotlin documentation on imports says
If there is a name clash, we can disambiguate by using
as
keyword to locally rename the clashing entity
So you could try to import the layouts with different aliases:
import kotlinx.android.synthetic.main.<layoutActivity>.* as lActivity
import kotlinx.android.synthetic.main.<layoutView>.* as lView
And use the textviews with the corresponding qualifier: lActivity.txtTitle
and lView.txtTitle
You can try below thing for TextView
having same id from different layout.
import kotlinx.android.synthetic.main.activity_main.text_hello as lActivity
import kotlinx.android.synthetic.main.extra_layout.text_hello as lView
use lActivity.text = "Some text"
for TextView
from activity_main
and lView.text = "Some text"
for TextView
from extra_layout
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With