19

How to access to view using kotlin synthetic extension if I have a layout like below:

file:two_days_view.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include
        android:id="@+id/day1"
        layout="@layout/day_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

    <include
        android:id="@+id/day2"
        layout="@layout/day_row"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

file: day_row.xml

   <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"       >

        <TextView
            android:id="@+id/dayName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

How to access to dayName? I looked for some like this:

day1.dayName.text = "xxx"
day2.dayName.text = "sss"

I see in Studio that I have access to dayName but to which one of dayName TextView is reference to?

Normal if I have only one included layout it works fine. But now I have multiple times included same layout.

of course I can always do:

day1.findViewById(R.id.dayName).text = "xxx"

but I'm looking for nice solution. :)

LunaVulpo
  • 3,043
  • 5
  • 30
  • 58

2 Answers2

52

As a general rule of thumb, you should not construct layouts that end up having multiple views with the same id - for this very reason.

But, to solve your problem: Instead of importing

kotlinx.android.synthetic.main.layout.day_row.*

you can import

kotlinx.android.synthetic.main.layout.day_row.view.* (Notice the additional .view at the end).

This will import the views not as properties on the Activity/Fragment level, but instead as extension properties for View. That way, you can do it the way you want, assuming that day1 and day2 contain the views you want:

day1.dayName.text = "xxx"
day2.dayName.text = "sss"
Robin
  • 3,682
  • 2
  • 22
  • 26
  • 3
    thx it wokrs. BTW: can you explain why shouldn't construct layouts in that way? If I have fixed identical 7 rows? Why should I make one huge xml with names day(1..7)name and so on. Any diff rent way to avoid huge xml? – LunaVulpo Feb 27 '17 at 08:11
  • @LunaVulpo That was more of a "disclaimer" from my side. I think in your case, with a fixed set of identical layouts, it's probably warranted, but in a lot of cases other solutions might be better suited, like making the layout a fragment or a custom view. I do realize that that's technically still putting views with the same IDs in the same resulting layout, but that way they're cleanly separated in code. – Robin Feb 27 '17 at 08:37
-2

In this case, use:

(day1 as TextView).text = ""
(day2 as TextView).text = ""
Morilo
  • 1