How do I import a file from the res folder in adroid studio with kotlin?

32 Views Asked by At

I created a recyclerView variable in an xml named activiy_main.xml and i want to access it in the MainActivity file I tried different ways to import the xml file but I get an error each time. I added a picture of the navigation in the project, what am I missing?

enter image description here

1

There are 1 best solutions below

1
Jamal N On

In your activiy_main.xml your recyclerview should look something like

<androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/my_recyclerview"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:orientation="horizontal"
                            app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
                            tools:listitem="@layout/featured_product_item"/>

in your activity you can access it like so:

val recyclerview = findViewById<RecyclerView>(R.id.my_recyclerview)