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. :)
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"
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