Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing views with the same id in different layouts with kotlin android extensions

In my Android project I have two layouts: num_info and num_info_pack. Both have views with id "circle". So I thought referencing those views by layout_name.circle would solve the problem:

val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false)
    inetView.circle.setBackgroundResource(background)

But circle is underlined with red and it says:

Overload resolution ambiguity. All these functions match.

public val View.circle: View! defined in kotlinx.android.synthetic.main.num_info_pack.view

public val View.circle: RelativeLayout! defined in kotlinx.android.synthetic.main.num_info_inet_plus_pack.view

Why is it confused about which circle I'm talking about if I'm specifically saying inetView.circle?

like image 252
Nazerke Avatar asked May 19 '17 04:05

Nazerke


2 Answers

In addition to the already very good answers, if you have the same IDs in multiple layouts in your project, it shouldn't matter which one you pick. Similar IDs, regardless of which layout it is defined, end up pointing to the same view. So, you can discard the other imports, leaving only the layout(s) that matters to you in the current activity/fragment/view

Hope that helps

like image 140
Kingsley Adio Avatar answered Oct 19 '22 19:10

Kingsley Adio


The solution here is in the imports. You must be importing two layouts like

import kotlinx.android.synthetic.main.num_info_pack

and

import kotlinx.android.synthetic.main.num_info_inet_plus_pack

Remove one of them and keep one with the appropriate layout file that you want to import. It should work fine.

like image 35
VipulKumar Avatar answered Oct 19 '22 18:10

VipulKumar