Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Kotlin's synthetic properties conflict

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 TextViews 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...

like image 986
Massimo Avatar asked Jan 23 '17 15:01

Massimo


People also ask

Is Viewbinding deprecated?

Kotlin Android Extensions is deprecated, which means that using Kotlin synthetics for view binding is no longer supported.

When was kotlin synthetics deprecated?

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.

What is Kotlinx Android synthetic?

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'

How does kotlin synthetic work?

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.


2 Answers

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

like image 97
Lukas Lechner Avatar answered Oct 12 '22 21:10

Lukas Lechner


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.

like image 40
paril Avatar answered Oct 12 '22 20:10

paril