Action Bar in API Level > 3.0 but Still Support API < 3.0
Asked Answered
S

3

1

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.

Springhouse answered 6/8, 2012 at 15:19 Comment(0)
S
1

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.

Springhouse answered 14/9, 2012 at 14:46 Comment(0)
D
3

What you want is ActionBarSherlock. It uses the native action bar for API >= 3 and provides backwards compatibility for API levels 2.x. There's also the Action Bar Compatibility sample project (listed as SupportAppNavigation, I believe). I think this is essentially the same thing.

Debutante answered 6/8, 2012 at 15:24 Comment(7)
I can't implement this without scrapping my map fragment support?Springhouse
@DiscoS2 - To provide fragment support pre-3.0, you need to be using the compatibility library. ActionBarSherlock is an extension of the compatibility library, so I don't see why you would need to scrap map fragments support. Can you explain your concern a little more?Debutante
To implement google maps along with fragment capability I had to already use an extension of the compatibility library so my concern is that I can't use both extensions of the original library?Springhouse
@DiscoS2 - ActionBarSherlock does not replace the compatibility library; it is built on top of it. I imagine that the google maps extension works the same way. If so, then unlikely (although possible, I suppose) that they will interfere with one another.Debutante
Ok thanks Ted. I guess I will give it a go. Bit of a hack though right? I just want the compatibility library to be fully complete and make supporting the different API levels seamless!Springhouse
Just had a look into it and the problem is, I can't extend the Activity using both SherlockFragmentActivity and the FragmentActivity in the google support library?Springhouse
@DiscoS2 - Looking at the sources for ActionBarSherlock, it seems SherlockFragmentActivity is derived (indirectly) from android.support.v4.app.FragmentActivity. It should be plug-compatible.Debutante
S
1

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.

Springhouse answered 14/9, 2012 at 14:46 Comment(0)
N
0

What I would recommend is to use ActionBarSherlock to get the tabbed action bar functionality.

I assume that you also need a MapView support in Fragment. For this I would recommend solution from MapView in a Fragment (Honeycomb) (look at user1414726 answer with sample code).

I think it is a better solution than using library though it is using deprecated LocalActivityManger. But in my opinion it is a better idea than using library where every Activity is a subclass of MapViewActivity which I assume you are using.

Neapolitan answered 6/8, 2012 at 15:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.