Sunday, January 20, 2013

Backward compatibility super quick

Setting up a project with ActionBar, NavigationTabs and swipe navigation in less than 2 minutes!

I have always appreciated how Action bar Sherlock provides the latest navigation features for the devices with lower versions of android , but only today did I realize that it is also super easy to configure. Make sure you have the latest versions of ADT and actiobarsherlock configured with your eclipse and follow the steps below to get you project running in less than 2 minutes.

Step 1.

In eclipse navigate to File->New->Other->Android Application Project. Enter the project details and set the Mininum Required SDK,Target SDK and Compile With with the highest available values(must be >11).
Click Next and setup the other values also in the following 2 screens.

Step 2.

Under the Create Activity screen,choose BlankActivity  and in the following screen select either of the options depending on how you want the navigation in your project.

Step 3

Delete the libs\android-support-v4.jar from the project and add the one present in the ActionBar Sherlock project.This is because the project might not compile if the versions of the jars mismatch.

Step 4
Navigate to project Properties->Android->Add Library and add the Action bar project.

Step 5 

Open the Activity's java file and replace the following:
FragmentActivity  ---> SherlockFragmentActivity
getActionBar ---> getSupportActionBar()
gettMenuInflater() --> getSupportMenuInflater()

Step 6 

Remove all imports from the java file and press ctrl+shift+o . Select the packages from the Sherlock Action Bar project for import when prompted.

Step 7

Open the AndroidManifest.xml file and change the minimun sdk version to 8 and the theme to Theme.Sherlock.Light.

This is all and your project would run like a charm on all versions of Android.



5 comments:

  1. Great post. Any thoughts on why the app crashes when I rotate my phone?

    ReplyDelete
  2. Great post Amol. Ernest, try update support library for both projects.
    Right click on project Android Tools -> Add Support Library...
    It works for me.
    Hope it helps.

    ReplyDelete
  3. Yep, that did the trick. I guess the v4 jar I had was out of date. Thanks!

    ReplyDelete
  4. Thanks guys! And yes this was a known issue with the older version of support library and has has been fixed now. Cheers!

    ReplyDelete