8

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?

Nazerke
  • 2,098
  • 7
  • 37
  • 57

3 Answers3

6

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

Kingsley Adio
  • 728
  • 5
  • 16
  • Really. Anyone care to share why this was downvoted? :O – Kingsley Adio Jun 07 '17 at 09:04
  • What you said is in fact correct. You just have to explicitly import from one of the layouts and it will end up pointing to the correct view regardless. – Edward van Raak Jul 26 '18 at 11:27
  • @KingsleyAdio it was exactly as you said. i just imported from one of the layouts and it figured it out. i had a custom view that takes different layouts. kotlin figures it all out by just importing any of the layout id. thanks alot. – j2emanue Oct 03 '18 at 08:25
  • @KingsleyAdio haha. At first time, I could not believe that as well. If you put some codes or detail explanations, it will carry conviction. Any way, I upvote for you. – zwh Jan 17 '20 at 14:44
6

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.

VipulKumar
  • 2,385
  • 1
  • 22
  • 28
2

I don't have android studio in hand now but I think this will solve your problem:

package XXX

import kotlinx.android.synthetic.main.num_info_inet_plus_pack.view.circle as inetViewCircle
import kotlinx.android.synthetic.main.num_info_pack.view as circle
//...
val inetView = activity.layoutInflater.inflate(R.layout.num_info_pack, parent, false)
inetViewCircle.setBackgroundResource(background)

Don't know if this will works because I can't test it right now. Please let me know whether it's working.

The problem is a name clash, so I think import alias may help.

Nazerke
  • 2,098
  • 7
  • 37
  • 57
glee8e
  • 6,180
  • 4
  • 31
  • 51
  • hey yes that's my current solution giving them different names. but there are quite a few cases like that. so it doesn't look tidy to have so many imports. Also, inetView is not kotlin synthetic object, it is an android object, so inetView.inetViewCircle doesn't work. inetViewCircle.setBackground.... works. – Nazerke May 19 '17 at 05:11
  • Uh, that's interesting. I'm deleting this anwser. Would you mind adding your workaround to your question body? – glee8e May 19 '17 at 05:12
  • You can leave this answer. This is workaround. I just edited it so now its ok – Nazerke May 19 '17 at 05:19