Mike T
Mike T

Reputation: 53

RecyclerView Kotlin, unable to add recycler widget to layout

I'm trying to implement RecyclerView in Kotlin. Following guides but getting an error right after launch "My Application keeps stopping"

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/my_recycler_view"
        android:scrollbars="vertical"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

Basically I just added Recycler widget to my layout. Nothing else changed from template empty activity starter project.

Upvotes: 0

Views: 158

Answers (3)

Mike T
Mike T

Reputation: 53

Figured out the problem, it was a mismatch on layout, recycler view needed more properties to match main layout:

   <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/myFirstRecyclerView"
        android:layout_width="0dp"
        android:layout_height="0dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"/>

Upvotes: 0

Atiq
Atiq

Reputation: 14825

The problem seems to be you are not using androidx version of RecyclerView

Dependency if not added already:

implementation 'androidx.recyclerview:recyclerview:1.0.0'

then replace your code with:

<androidx.recyclerview.widget.RecyclerView
    android:id="@+id/my_recycler_view"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>

Also in your Activity/fragment make sure your are using

import androidx.recyclerview.widget.RecyclerView;

Upvotes: 2

Alan Deep
Alan Deep

Reputation: 2105

Common mistake is not adding app:layoutManager to your RecyclerView in your XML layout:

app:layoutManager="android.support.v7.widget.LinearLayoutManager"

Upvotes: 0

Related Questions