Friday 11 July 2014

Using RecyclerView and CardView in Eclipse ADT

Hello!

In my recent attempts to integrate a RecyclerView and CardView into an application, I ran into a few technical snags which caused the application to keep crashing. After a quick google search, it became quickly obvious that this was a problem faced by a lot of people and, furthermore, most solutions provided a method for users of Android Studio and nothing for Eclispe ADT users like myself. After some more digging around I found a post on stack exchange which helped resolve my issue. However, it took some tinkering around before I could get my head around what the post asked me to do. So this is my attempt to create an idiot proof guide to help newcomers to the field of Android Development like myself to create and use the RecyclerView and CardView features.

The RecyclerView and CardView packages are part of the Android L Developer Preview version of Android. However, with the  use of compatibility support libraries, we can include these features in older versions as well. Here's how:

Step 1: Download and install the Android Support Libraries using the SDK manager.




Step 2: Extract the required files from the AAR file from the package downloaded.

2.1 Once you have the libraries downloaded, browse your installation directory to the path:

...\adt-bundle-windows-x86_64-20140702\sdk\extras\android\m2repository\com\android\support

Now we need to extract the required files from these directories for both the CardView and the RecylerView. I'll demo how its done for CardView. Pretty much the same steps must be followed for RecyclerView.

2.2 Open the cardview-v7/22.0.0-rc1 folder. Locate cardview-v7-21.0.0-rc1.aar, change its extension to .zip and extract the contents into the folder.

2.3 Rename the classes.jar file to something meaningful like cardview-v7.jar


2.4 Repeat the above steps for recycleview-v7 folder to obtain recyclerview-v7.jar.

Step 3: Setup a Library Project in Eclipse and set it up to serve as our homemade library

3.1 Open Eclipse ADT and create a new library project. (to create library projects check out: Vogella Library Project Tutorial)

Set the fields as Indicated.

3.2 Copy required files into library project and set up Build Paths


Copy the AndroidManifest.xml file from the extracted folder of cardview-v7-21.0.0-rc1 (from step 2) and replace the generated AndroidManifest.xml file of your library project.

Copy the values.xml file from the extracted folder of cardview-v7-21.0.0-rc1/res/values (from step 2) and paste it in the /res/values of your library project.


Copy the cardview-v7.jar and recyclerview-v7 obtained from step 2 into your project's libs folder.
They should now appear in the project explorer pane when you expand the project->libs folder
(refresh the project explorer if it doesn't appear after copying to the directory.)
Right click on cardview-v7.jar and select Build Path->Add to Build Path
Repeat for recyclerview-v7.jar
Then, right-click on your project root and select Build Path->Configure Build Options and select the checkboxes next to the added libraries and click on OK.





Step 4: Include created library into desired project for integration.

4.1 Import the library project into the workspace of the required project.4.2 Set Project References.

Right click on project root->Properties and select the Android subsection and select Add.


Add the custom library we created as a reference for the project.
And that's it! Enjoy the RecyclerView and CardView feature set.

Notes:

1. The libraries included this way will require manual updating once newer releases are out.
2. The minSDK and tagetSDK must be set to "L" in the Library Project we created.
3. This is not a complete solution but more of a hack to make things work till Google releases the official release version


References:

These resources helped me find a way around the issues I faced.
1. Stack Overflow Article (Link)
2. Android Developer Preview Reference (Link)

2 comments: