54

Hi I'm new to Android development and I'm trying out a tutorial on MVVM I found on YouTube. The example project in the video uses AppCompat but I converted mine to androidx because from what I read its the current(?) version to use? Am I mistaken with this thinking?

Anyways Part of the tutorial makes use of a RecyclerView and I can't access it on my activity_main.xml file stating that v7 is an unresolved package. android.support.v7.widget.RecyclerView shows up with v7 onwards as red text. I know I can revert it back to older versions but I guess I am trying to make this work since moving forward its expected to know how to use androidx right?

I don't know how to add the RecyclerView to the project with my current project migrated to androidx.

What I've tried:

  • Adding implementation 'com.android.support:recyclerview-v7:28.0.0' based on the docs
  • Invalidating cache and restarting

My Dependencies:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.1.0-alpha04'
    implementation 'androidx.appcompat:appcompat:1.0.0-beta01'
    implementation 'androidx.cardview:cardview:1.0.0'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.1.0-alpha4'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.1.0-alpha4'

    //RecyclerView
    implementation 'com.android.support:recyclerview-v7:28.0.0'


    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:2.1.0-alpha04"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:2.1.0-alpha04"

    // Room Components
    implementation "androidx.room:room-runtime:2.1.0-alpha06"
    annotationProcessor "androidx.room:room-compiler:2.1.0-alpha06"
}

My activity_main.xml

<?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">

    <view class="android.support.v7.widget.RecyclerView"
        android:id="@+id/recycler_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:listitem="@layout/todo_item"/>

</androidx.constraintlayout.widget.ConstraintLayout>
Martin Zeitler
  • 1
  • 19
  • 155
  • 216
kobowo
  • 2,529
  • 2
  • 24
  • 37
  • 2
    Please don't tag questions with IDE tags (android-studio) just because you use that IDE: these tags should only be used when you have questions about the IDE itself, and not any code you write (or want to write) in it. See [when is it appropriate to remove an IDE tag](https://meta.stackoverflow.com/a/315196/6296561), [How do I avoid misusing tags?](https://meta.stackoverflow.com/questions/354427/how-do-i-avoid-misusing-tags), and the [tagging guide](https://stackoverflow.com/help/tagging). Also, please read [How to Ask](https://stackoverflow.com/help/how-to-ask) – MD Naseem Ashraf Apr 14 '19 at 16:51
  • why you've added `appcompat` twice? using `alpha` & `beta` versions is especially pointless, when nothing is working. this question should be tagged [beta-testing](https://stackoverflow.com/questions/tagged/beta-testing). – Martin Zeitler Apr 14 '19 at 17:30
  • You should know the difference between AndroidX and Android Support Libraries (numbered versions). When using AndroidX, do not use any of the numbered support libraries. Read this AndroidX [documentation](https://developer.android.com/jetpack/androidx) before you use AndroidX, to get a rough idea of how & why to use it. You don't seem to know how dependency versions work in Android. Here is a [good blog](https://android.jlelse.eu/what-is-the-difference-between-canary-beta-rc-and-stable-releases-in-the-android-studio-bbbb77e7c3cf) to give you a quick overview of version names of libraries. – MD Naseem Ashraf Apr 15 '19 at 08:40

6 Answers6

114

RecyclerViewwas migrated to AndroidX as well:

  1. Updatebuild.gradle:
implementation 'androidx.recyclerview:recyclerview:1.1.0'
  1. Change layout file:
<androidx.recyclerview.widget.RecyclerView>...</androidx.recyclerview.widget.RecyclerView
iamkdblue
  • 3,448
  • 2
  • 25
  • 43
S-Sh
  • 3,564
  • 3
  • 15
  • 19
  • 1
    Wow thanks for the fast response! So I just googled the docs and I'm just wondering...are the docs updated with the latest version (like androidx)? or was I really just that bad at searching? – kobowo Apr 14 '19 at 15:43
  • 1
    Now better use `Refactor` -> `Migrate to AndroidX...` menu option in Android Studio to do the conversion since mixed type(AndroidX and Support use in same project) will not able to build, ref: https://stackoverflow.com/a/54533702/1074998 . And also need do global search for `support` keyword to replace the missing conversion manually by referring https://developer.android.com/jetpack/androidx/migrate – 林果皞 Jul 25 '19 at 19:32
  • 2
    I wonder why they don't change the official documentation : https://developer.android.com/guide/topics/ui/layout/recyclerview#add-support-library – Darkoob12 May 10 '20 at 15:59
  • For me it only worked with: implementation 'androidx.recyclerview:recyclerview:1.0.0' – Craig Grummitt Sep 22 '20 at 09:55
5

Official document : https://developer.android.com/jetpack/androidx/migrate

Step 1: Check and set the lines in gradle.properties:

android.useAndroidX=true
android.enableJetifier=true

Step 2: Change

implementation 'com.android.support:recyclerview-v7:28.0.0'

to

implementation 'androidx.recyclerview:recyclerview:1.0.0' //Update to latest version

And finally: Change the tag

 <view class="android.support.v7.widget.RecyclerView"...>

to

<androidx.recyclerview.widget.RecyclerView
     android:id="@+id/recycler_view"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     tools:listitem="@layout/todo_item"/>
Wilson Tran
  • 4,050
  • 3
  • 22
  • 31
3

you can use the materials design Dependencies

implementation 'com.google.android.material:material:1.2.0-alpha04'
implementation 'com.android.support:multidex:1.0.3'
zakaria
  • 365
  • 2
  • 5
  • 12
1

If you follow the above instructions and the RecyclerView still appears as a grey box in the layout preview, try rebuilding your project.

Cherif Diallo
  • 311
  • 2
  • 3
1

Usually just right-click on build.gradle (Module: app) > Refactor > Migrate to AndroidX. To migrate and rename all the dependencies automatically.

1

This issue is solved by adding Dependency to the build.gradle(Module:App):

implementation 'androidx.recyclerview:recyclerview:1.1.0'

and Adding this code to your preferred layout in order to use RecyclerView

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

here is the link to the doc: https://developer.android.com/guide/topics/ui/layout/recyclerview

Sarwar Sateer
  • 395
  • 3
  • 16