62

I'd like to use the new CardView widget that was introduced with the new Android L Developer Preview Support Library (As mentioned here), which seems to be part of a new revision of the v7 support library.

I have now updated my SDK version, downloaded the newest Support Library package, but I still cannot find the new CardView widget.

I already searched the web and the official docs, but could not get any hints on where I can get the new support library.

Any help is highly appreciated!

EDIT: I'm using ADT/Eclipse

Mycoola
  • 1,135
  • 1
  • 8
  • 29
FD_
  • 12,947
  • 4
  • 35
  • 62
  • There is a note in Android Developer website, maybe it helps: The Eclipse ADT plug-in requires Java 7 if your compilation target is the L developer preview. – adboco Jun 27 '14 at 10:35
  • @FD_: I'm curious why you're still using Eclipse. What's wrong with Android Studio? – dannyroa Sep 09 '14 at 23:03
  • 2
    @dannyroa: I switched in the meantime ;) At the time of posting the question, I did not want to switch because at that time AIDE, the Android IDE did not support the new project structure to a usable degree. – FD_ Sep 10 '14 at 07:52
  • @FD_: Haha! I hope your life is better with AS. Mine is. – dannyroa Sep 12 '14 at 17:43
  • I didn't know CardView needs its own compatibility library. I assumed AppCompat would have handled this view as well. – Price Mar 09 '15 at 15:41

12 Answers12

52

Using Gradle or Android Studio, try adding a dependency on com.android.support:cardview-v7.

There does not seem to be a regular Android library project at this time for cardview-v7, leanback-v17, palette-v7, or recyclerview-v7. I have no idea if/when Google will ship such library projects.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for the answer! The problem is I'm still using ADT/Eclipse. Is there any way using it there? – FD_ Jun 27 '14 at 10:28
  • @FD_: Not presently -- that's what my second paragraph was referring to. I also don't see this stuff out in the Android source repo, so it's not like we can package up our own library projects. AARs cannot readily be converted into their original library projects, as they contain compiled code with numeric references to `R`-style constants. – CommonsWare Jun 27 '14 at 10:32
  • Okay. Thanks then, will test using Android Studio. – FD_ Jun 27 '14 at 10:51
  • 2
    I finally found a way to do it! It was no problem to copy the resources from the AAR into a library project in Eclipse and as the included sources were kept in their own jar, I could simply re-use that as well. I've added my own answer. – FD_ Jun 28 '14 at 15:09
  • Addesd in android studio and worked perfect! Thanks! – Shudy Dec 24 '14 at 11:54
41

I finally found a way to use CardView in ADT/Eclipse. It's actually pretty easy:

  1. Create a new project in Android Studio
  2. Add the CardView dependency as explained in the other answers to this question
  3. Open ADT and create a new library project with package name android.support.v7.cardview
  4. Delete all resources ADT auto-created
  5. Find the exploded-aar folder in Android Studio and copy the following files to these locations:
    • res/values/values.xml to the same location in your ADT project
    • classes.jar to libs/ in your ADT project
    • AndroidManifest.xml use it to replace the auto-generated manifest in ADT
  6. Add classes.jar to the build path and make sure it's exported
  7. Add a reference to the library project in the project you want to use CardView in. You can follow the steps provided under Adding libraries with resources here: https://developer.android.com/tools/support-library/setup.html

As an alternative to having to create a new Android Studio project in order to get the AAR's content, you could also simply find and unzip the AAR from the local maven repo. Just follow the steps provided by Andrew Chen below.

Please note the CardView library might not be available in source- and ADT-compatible-form because it's still only a preview and a WIP. As there might be bug fixes and improvements in following releases, it's important to keep the library up-to-date, which is easy using the Gradle dependency, but must be done manually when using the steps provided above.

FD_
  • 12,947
  • 4
  • 35
  • 62
  • 1
    Your step #5 refers to Studio. Is step #1 supposed to be "create a new project in Android Studio"? – CommonsWare Jun 28 '14 at 15:42
  • 1
    Also, `classes.jar` will have compiled references to the AAR's value for `R` constants, and I fail to see how this approach will do anything to teach Eclipse to not change those `R` constants. – CommonsWare Jun 28 '14 at 15:49
  • 1
    @CommonsWare Right, I corrected #1, #5 should be right. – FD_ Jun 28 '14 at 16:45
  • 1
    @CommonsWare Hm, actually, I naively just tried copy-pasting and it worked. As far as I understand, AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name), so I assumed AAPT would generate the same identifiers as they are contained in the AAR's jar. – FD_ Jun 28 '14 at 16:48
  • "AAPT always generates the same resource identifier for the same resource (Package, Res Type and Name" -- not that I am aware of. – CommonsWare Jun 28 '14 at 16:50
  • Okay, right, that does not apply when the project references library projects. However, it seems there are no compiled references in the jar. The classes inside that jar get their resource identifiers from the R.java that is generated for the library project in Eclipse. – FD_ Jun 28 '14 at 17:54
  • Very interesting. Maybe it's something with the explosion process. I had looked at trying to find a way to get something from an AAR that could be used in Eclipse and I had convinced myself that the precompiled-`R` references would be a show-stopper. I'll try your technique next week sometime (after the dust settles from all the tools explosions this week). Many thanks! – CommonsWare Jun 28 '14 at 17:57
  • 2
    I finally got an opportunity to try this on a more complex example, `mediarouter-v7`, which has a ton of resources. And it seems to work (the crash I was getting I also get when compiling against the AAR and seems to be something out of sync between Play Services and Chromecast in my test environment). In my earlier research, I got seriously concerned by the `R.txt` file, which lists a bunch of resource IDs. I'm not sure what it's there for, but it doesn't seem to be required for converting an AAR->library project. Nice work! – CommonsWare Jul 02 '14 at 22:21
  • @CommonsWare yeah, was a bit confused by the R.txt initially as well. Anyway, glad it worked for you as well! I'm sorry to ask, but could you possibly have a look at another question of mine on Proxying R.java? (http://stackoverflow.com/questions/24534892/proxying-r-java) – FD_ Jul 03 '14 at 07:03
  • Sorry, I haven't a clue on that proxying bit. With regards to your answer, I cobbled together a Ruby script to automate the conversion, and I have written a blog post about your recipe and the script: http://commonsware.com/blog/2014/07/03/consuming-aars-eclipse.html Again, nice work! – CommonsWare Jul 03 '14 at 13:04
  • @CommonsWare Okay, thanks for answering anyway! Let me express what great honour it is to be mentioned on your blog (I'm a regular reader)! – FD_ Jul 03 '14 at 13:28
  • @FD_ Why can't Google provide normal samples as it used to , to support Eclipse users too? – android developer Jul 05 '14 at 19:11
  • @android developer: Well, I guess they either want to force devs to Android Studio (You really should consider changing if you don't have any good reasons to stay with ADT), or they just don't want to release two versionsas long as the library is still in preview. – FD_ Jul 05 '14 at 20:02
  • @FD_ too bad. Google seems to neglect Eclipse . I really like Eclipse, and I've tried Android studio before. It had very cool features, but it's not comfortable compared to Eclipse and isn't friendly for people moving from Eclipse. Plus it doesn't support any plugin of Eclipse. – android developer Jul 05 '14 at 20:07
  • @android developer: I tried AS once when the first preview was released and found it so buggy that I gladly sticked with Eclipse. However, I gave it another try since I wrote the question, and it worked quite stable, so I finally did the move. AS has received an Eclipse-project importer since then that worked flawlessly for my projects. – FD_ Jul 05 '14 at 20:15
  • @FD_ I see. Maybe I will try again. Does it support NDK now? the documentation seems a bit old and maybe things have changed. Also, how important is it to learn how to use Gradle on AS ? – android developer Jul 05 '14 at 21:03
  • @android developer: No, NDK is not supported yet but announced to be coming soon. I highly recommend learning the new gradle build system. It just takes a few hours to understand the basics (eg different build types and product flavors are really cool). – FD_ Jul 06 '14 at 11:01
  • @FD_ ok, thank you. i will try it. still sad about Eclipse being neglected. – android developer Jul 06 '14 at 14:20
  • @FD_ any solution found now,on how to use Cardview using Eclipse. – Dory Nov 05 '14 at 12:44
  • @Dory : in the latest android-sdk folder "\extras\android\support\v7\", we now have "cardview" folder. Follow the above steps given in the answer. Instead of step 5, import the project from "cardview" folder. Rename it to "android-support-v7-cardview". Set it as lib. And then import the same lib in your eclipse project. – SHS Dec 02 '14 at 08:17
  • @FD_ is this still the process to follow? I've just got a similar error and I'm kinda surprised this is so complex even now nearly a year after the preview went out. – vkislicins Apr 29 '15 at 14:35
  • @vkislicins Yes, as far as I know, if you are still using Eclipse, you still have to follow this procedure. I switched to Android Studio though. – FD_ May 01 '15 at 08:22
  • @FD_ please answer this too: http://stackoverflow.com/questions/36393540/no-shadow-elevation-underneath-second-card-if-there-are-two-card-in-the-layout –  Apr 04 '16 at 03:09
33

You need to add this in your build.gradle:

dependencies {
    ...
    compile 'com.android.support:cardview-v7:+'
}

And then Sync Project with Gradle Files. Finally, you can use CardView as it's described here.

adboco
  • 2,840
  • 1
  • 21
  • 21
27

I have done following and it resolve an issue with recyclerview same you may use for other widget as well if it's not working in eclipse project.

• Go to sdk\extras\android\m2repository\com\android\support\recyclerview-v7\21.0.0-rc1 directory

• Copy recyclerview-v7-21.0.0-rc1.aar file and rename it as .zip

• Unzip the file, you will get classes.jar (rename the jar file more meaningful name)

• Use the following jar in your project build path or lib directory.

and it resolve your error.

happy coding :)

Rajat Sharma
  • 676
  • 4
  • 6
  • 4
    That will not work if the AAR in question has resources. Now, it so happens that the `RecyclerView` AAR does not presently have resources, and so this works, but that's not assured for future versions of the AAR. – CommonsWare Jul 02 '14 at 21:27
19

https://github.com/yongjhih/CardView

A CardView v7 eclipse project. (from sdk/extras/android/m2repository/com/android/support/cardview-v7)

The project was built by steps:

cp {sdk}/extras/android/m2repository/com/android/support/cardview-v7/21.0.0-rc1/cardview-v7-21.0.0-rc1.aar cardview-v7-21.0.0-rc1.zip
unzip cardview-v7-21.0.0-rc1.zip
mkdir libs/
mv classes.jar libs/cardview-v7-21.0.0-rc1.jar
Andrew Chen
  • 447
  • 7
  • 14
10

I was able to work it out only after adding those two TOGETHER:

dependencies {
...
implementation 'com.android.support:recyclerview-v7:27.1.1'
implementation 'com.android.support:cardview-v7:27.1.1'
...
}

in my build.gradle (Module:app) file

and then press the sync now button

AG1
  • 6,648
  • 8
  • 40
  • 57
Roy Doron
  • 585
  • 9
  • 23
3

Although a bit hidden it's in the official docs here where can the library be found among the sdk's code, and how to get it with resources (the Eclipse way)

Eloi Navarro
  • 1,435
  • 1
  • 14
  • 26
2

From: https://developer.android.com/tools/support-library/setup.html#libs-with-res

Adding libraries with resources To add a Support Library with resources (such as v7 appcompat for action bar) to your application project:

Using Eclipse

Create a library project based on the support library code:

  • Make sure you have downloaded the Android Support Library using the SDK Manager.

  • Create a library project and ensure the required JAR files are included in the project's build path:

  • Select File > Import.

  • Select Existing Android Code Into Workspace and click Next.

  • Browse to the SDK installation directory and then to the Support Library folder. For example, if you are adding the appcompat project, browse to /extras/android/support/v7/appcompat/.

  • Click Finish to import the project. For the v7 appcompat project, you should now see a new project titled android-support-v7-appcompat.

  • In the new library project, expand the libs/ folder, right-click each .jar file and select Build

  • Path > Add to Build Path. For example, when creating the the v7 appcompat project, add both the android-support-v4.jar and android-support-v7-appcompat.jar files to the build path.

  • Right-click the library project folder and select Build Path > Configure Build Path.

  • In the Order and Export tab, check the .jar files you just added to the build path, so they are available to projects that depend on this library project. For example, the appcompat project requires you to export both the android-support-v4.jar and android-support-v7-appcompat.jar files.

  • Uncheck Android Dependencies.

  • Click OK to complete the changes.

  • You now have a library project for your selected Support Library that you can use with one or more application projects.

  • Add the library to your application project:

  • In the Project Explorer, right-click your project and select Properties.

  • In the category panel on the left side of the dialog, select Android.

  • In the Library pane, click the Add button.

  • Select the library project and click OK. For example, the appcompat project should be listed as android-support-v7-appcompat.

  • In the properties window, click OK.

I'm_With_Stupid
  • 1,112
  • 4
  • 16
  • 35
2

Maybe it's a little bit late to add answer here. But I think this answer will help the later ones and especially those who don't want to use Android Studio.

Although the documents says that RecyclerView and CardView are part of v7 appcompat library. But as I tried and found, RecyclerView and CardView are actually depend on v7 appcompat library. So if you want to use RecyclerView or CardView, you need to add both v7 appcompat library and RecyclerView/CardView.

Referencing the link here, if you want to use CardView in your Eclipse android project, you need to import both v7 appcompat library and CardView into Eclipse workspace and make them as library projects. Then make CardView project depends on v7 appcompat library project and make your project depends on CardView project.

Michelle
  • 1,097
  • 8
  • 18
2

Simply add the following line in your build.gradle project

dependencies {
    ...
    compile 'com.android.support:cardview-v7:24.0.0'
}

And sync the project with gradle.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Pihu
  • 1,041
  • 11
  • 35
0

I did what FD_ said and it crashed with errors as it was looking for "Landroid/support/v7/cardview/R$styleable;", which was not compiled with it

If you really want to use CardView before in eclipse before it gets its own library, you can extract the classes from the classes.jar, copy and paste them into your project, with the values.xml from above from Android Studio and change all the references to android.support.v7.R to yourpackagename.R in the copied classes. This worked and ran for me

Kieron
  • 1,982
  • 2
  • 18
  • 17
  • What package name did your Eclipse library project have? Did the auto-generated R.java in your Eclipse library project contain the tag from the AAR's res/values/values.xml file. I had the same crash initially but resolved it by correcting the library project's package name. How did you extract the classes from the jar? Did you decompile it? – FD_ Jun 28 '14 at 21:23
  • It did have the same name, so it might have just been a quirk, and yes it did have the correct resources. I decompiled the JAR using a standard decompiler (JD) – Kieron Jun 29 '14 at 08:20
0

In the most latest android, we implement card view like this:

build.gradle file:

dependencies {
    ...
    compile 'com.android.support:cardview-v7:24.0.0'
}

and in the xml file its referenced as:

<androidx.cardview.widget.CardView
…>
…
</androidx.cardview.widget.CardView>

this means its class has changed to: androidx.cardview.widget.CardView

user3044482
  • 401
  • 4
  • 11