Wednesday, September 4, 2013

How to Run the Android Support Library Samples (Eclipse Version)

 How to Run the Android Support Library Samples (such as ActionBar) in Eclipse

First, make sure you have installed the necessary plugins for Android Development such as ADT.  Here is the official information from Google on how to get the plugins installed in Eclipse.

What are the Android support libraries?  They are .jar files or eclipse projects that let you access the latest features in the Android SDK on older devices such as Fragments (v4) or ActionBar (v7) support.  The v4 library requires a minimum SDK of 4 or Android 1.6, the v7 library requires a minimum SDK of 7 or Android 2.1, and so on.

So you want to support the older Android phones and start by running the support library samples (v4, v7, v13, AppNavigation, ActionBar, etc), but can't find the instructions from Google?  You'll need to download the Android Support Library, create a blank library project, import the existing android code (for v7 support), fix up the created library projects, create a blank project, import the sample demo code, and finally link the support library to your demo.

Also, there is a gotcha to watch out for when using the ActionBar that I experienced where the android:showAsAction values in your menu.xml are ignored and must be set at runtime.  See below for details.

Get the Support Library

Load the support package (and at least API 19) through the Android SDK Manager

After the download is finished, this will be in:{android_sdk}\extras\android\support\
The {android_sdk}\extras\android\support\samples\ folder contains the samples that we will import.  The path to your SDK is listed under SDK Path in the Android SDK Manager tool.
The v4, v7, v13 include the support library for whichever minimum SDK you are willing to support.  i.e. when using the v7 library, the min SDK should be set to 7.  The min SDK should be 13 when using the v13 library.

To install the Android Support v4 Sample Demo

Follow this excellent guide by Tek Eye.  The guide by Tek Eye allowed me to install the samples and get them going, but it did not have an entry for the Android Support v7 Demos, which are a bit trickier to get going.
NOTE: The latest copy of the Android Samples v4 required setting the build target to API 19 or removing the attribute in the Support4Demos/res/drawable/ic_drawer.xml marked "android:autoMirrored='true'"

To install the Android Support v7  Samples Demo

Create a New Eclipse Project to Store the v7 Support Library

You'll be creating a blank project and using this to import the support library as an android library project.  Choose File / New / Android Application Project

Uncheck the 'Create custom launcher' and the 'Create activity' icon, but
Check the 'Mark this project as a Library'

Now, load the support library.  Select File / Import... and choose 'Existing Android Code into Workspace'
Browse to the {android-sdk}\extras\android\support\v7 folder and select Copy projects into workspace if you want.

That will give you 4 new projects in your Eclipse workspace.  But they are not quite ready to use yet.  Also the android-support-v7-mediarouter will have an error that you will need to resolve.

To Fix Up the Android Support Library v7 Dependancies

You should now have 4 new projects:

Expand the libs folder of each library, right-click on each jar and select Build Path / Add to Build Path

Now you will also need to export the jars when projects are linked against this library

Go to Build Path / Configure Build Path and on the 'Order and Export' tab, check the support jars and uncheck Android Dependencies.  Do this for each library: android-support-v7-appcompat, android-support-v7-gridlayout, & android-support-v7-mediarouter.

Now, at this point, android-support-v7-mediarouter will still have a red mark on it with the error: error: Error retrieving parent for item: No resource found that matches the given name 'Widget.AppCompat.ActionButton'.

Link the android-support-v7-appcompat library to the android-support-v7-mediarouter

The android-support-v7-mediarouter has some dependencies on the android-support-v7-appcompat library, so we will need to link the projects.

Click on properties of the android-support-v7-mediarouter project. 
Select the Android page
Add... the android-support-v7-appcompat library

Now do a Project / Clean / All and the Support Library v7 should be clean of errors and your support library is ready to go.

NOTE:  The support library install instructions were adapted from the Android developer page here.

Create a New Eclipse Project to Store the v7 Demo Sample

Choose File / New / Android Application Project

Uncheck the 'Create custom launcher' and the 'Create activity' icon.

Right Click on the project and select Import...  
Pick General / File System

Select the {android-sdk}\extras\android\support\samples\Support7Demos
Check the res, src, and manifest files.

Link the project to the Android Support Library v7:
Click on properties of the SupportV7Demo project. 
Select the Android page
Add... the android-support-v7-appcompat library
Add... the android-support-v7-gridlayout library
Add... the android-support-v7-mediarouter library

Wow, now that all those steps are completed you should be able to compile and install the Android Support Samples to your Android device.

That's it!  The same technique can be used to install the Android Support v4 Library and Samples or the App Navigation Samples.

Note: As one observant reader noticed below, this leaves you with an extra AndroidSupportV7 library project that Eclipse created during the import process that is not used.  You can delete it, or add the libraries above (appcompat/gridlayout/mediarouter) into it so that you only need to import one combined project instead of three separate ones.

General Support Library Notes:

One anonymous comment noted if you are having troubles with a project, check that you have the Target SDK Version in the manifest set to at least 19.  This is found in Eclipse under Project properties / Android / Project Build Target to API 19 or higher.

To use the v7 files, copy these to your project's /libs folder.
  •     android-support-v4.jar
  •     android-support-v7-appcompat.jar
  •     android-support-v7-gridlayout.jar (*if needed)
  •     android-support-v7-mediarouter.jar (*if needed)

Typical gotchas for people:
  •     Make sure the min SDK is at least API 7
  •     Make sure the project target build is at least API 19

What to do next:

To learn more about database and network usage with android, check out the WorxForUs Android Database and Networking framework tutorial that assists database access and network access and addresses several common pitfalls.

Hope you found this guide useful, please drop a note, funny internet cat picture, or +1 if it helped.

ActionBar Gotcha Notes:

If you have trouble getting the ActionBar to display icons instead of just overflowing, check where you are setting the showAsAction values.  In the menu_layout.xml, the showAsAction attribute is ignored when using the support library and won't work.    <item
        android:showAsAction="ifRoom" //this will not work

To fix this, add the show as action items in the options menu creation function
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(, menu);
        MenuItem add = menu.getItem(0);

        //Set the show as action value here at runtime
        MenuItemCompat.setShowAsAction(add, MenuItemCompat.SHOW_AS_ACTION_IF_ROOM);


  1. Thanks for this. THere seems to be a small typo in the third last paragraph:

    Select the {android-sdk}\extras\android\support\samples\Support4Demos

    be Support7Demos?

  2. Yes, it should - I'll update the instructions to correct it. Thanks for the note, Andrew.

  3. Hi,
    thanks for this how to. Now this finally works. That's great but I do not get why do you need your own support library project (i.e. AndroidSupportV7). I can not see any connection between the Support7Demos and the AndroidSupportV7 project.

    1. You're right, you don't need it. There is no connection between AndroidSupportV7 and the Support7Demos. It's an artifact of how I imported the project with Eclipse. By doing that we only had to import once instead of 3 times (once for each sub-project), but it leaves us with an extra library that is not used.
      You can delete it, because the sub-libraries v7-appcompat, v7-gridlayout, & v7-mediarouter are all that you will need.

      Thanks for reading!

  4. HI,
    I followed the steps exactly but still 'mediarouter' has the following errors:

    error: Error: No resource found that matches the given name (at 'paddingEnd' with value '?android:attr/listPreferredItemPaddingEnd'). mr_media_route_list_item.xml /android-support-v7-mediarouter/res/layout-v17

    error: Error: No resource found that matches the given name (at 'paddingStart' with value '?android:attr/listPreferredItemPaddingStart'). mr_media_route_list_item.xml /android-support-v7-mediarouter/res/layout-v17

    error: No resource identifier found for attribute 'paddingEnd' in package 'android' mr_media_route_list_item.xml /android-support-v7-mediarouter/res/layout-v17

    error: No resource identifier found for attribute 'paddingStart' in package 'android' mr_media_route_list_item.xml /android-support-v7-mediarouter/res/layout-v17

    1. Version 17 of the support library and API 17 introduce the paddingStart/End that you are having trouble with. Check that you have the API 17 SDK downloaded from the Android SDK Manager.

      The android-support-v7-mediarouter depends on the android-support-v7-appcompat project, check that the library reference is set in the Eclipse Android project properties and that android-support-v7-acccompat compiles properly.

      Your projects should target API of at least 17 and min SDK of at least 7.

      You may just need to do a clean all.

  5. Still the mediarouter showing error on Layout-v17

    how can i fix this , Please help

    1. When you right click on the project and select Properties / Android, which Project Build Target do you have selected? Are you having the same problem as anonymous?

      I wonder if someone else is also having this problem. Since the error references the v17 it leads me to think that the project is having trouble finding the API17 files. Post your error console if possible.

    2. Hey, I had the same issue and fixed it by changing the "Project Build Target" to "Android 4.2.2" or higher in the project properties. You can change it by right clicking on the project, clicking properties, clicking Android in the left pane of the new window, then checking the appropriate option. Like the poster said, mediarouter requires API 17+, so picking any of those should work. Happy programming :)

  6. thank you for this tut . but how can use this SupportV7Demo project in my own project that support android 2.1 and above ?

    1. The recommended way of getting the base support library into Eclipse projects is to Right-click on your project, go to Android Tools / Add Support Library...

      You can also copy the android-support-v7-appcompat.jar, and other v7+ support librarys into your project from where you have the SDK installed at \android-sdk-windows\extras\android\support\v7

      Note: If you are using the v7 support package your min SDK in the manifest must be API7 which is Android 2.1. You can go as low as 4 if you only use the v4 support package.

  7. thank you very much

  8. Add to this the various vendors providing different phones with numerous variations of hardware components means that most developers have nightmares developing code for each individual phone rather than a universal app.gerald winata gozali

  9. best roti maker. Still the mediarouter showing error on Layout-v17

  10. est place you have ever met. Best sad shayari

  11. Awesome Blog!!! Thanks for sharing this data with us... oracle training in chennai