Search code examples
androidandroid-fragmentsandroid-actionbarandroid-mapsandroid-compatibility

Action Bar in API Level > 3.0 but Still Support API < 3.0


Yet another compatibility question.

THE PROBLEM

I need to use a tabbed action bar in api level 3.0 and greater to switch between fragments. However, I also need to be able to switch between these fragments somehow in api level < 3.0.

The spanner in the works is the fact that I have already downloaded a custom compatibility library that allows me to use google maps with fragments and therefore I can't use a library.

THE QUESTION

How can I implement a tabbed ActionBar solution in 3.0 and greater and also cater for the bigger market that is 3.0 and less?

Any help would be grand.


Solution

  • So I managed to figure a solution a while ago, it's just taken me a while to post it here.

    This is the solution that works for me. I needed to use an ActionBar but I also needed to use the MapActivity as well as the FragmentActivity hence not being able to use ActionBarSherlock.

    What I did was the following:

    • Created a fragment called CCActionBar which handles the touching of the CCTab's by adding itself as a listener(explained below). It also inflates a layout called action barlayout which has the tabs and images arranged, just reference the tabs at run time.

    • Created a custom view called CCTab which represent the tabs of the action bar. When its touched it tells its listener (CCActionBar) its been touched.

    • In my main layout xml file have an action bar container which I show or hide at runtime depending on the API level.

    • Now in my main activity in my OnCreate method I check whether my API level is greater than 3.0 or not. If it is I just implement the standard ActionBar making sure my custom action bar container is invisible. If it isn't I set up my custom action bar and make sure my action bar container in my xml layout is visible.

      • Then I make sure that when a tab is pressed whilst using either method it is handled in the same way so once its setup you don't have to handle it any differently.

    I hope this helps someone somewhere or at least give you an idea of how to proceed.