Reputation: 18130
So I have a few questions about implementing the Android action bar. Up to this point I haven't really made any apps that were (in my opinion) good enough to put on the market. This really seems to be my inability to understand how the layouts and action bar will work in different versions of Android.
Of course Google seems to be the best bet for this, but I come across a few grey areas that maybe someone can clear up. I can see that the Action bar was implemented in API level 11, but then what happens when I try to run it on a device lower than 3.0? Also, this seems to be the most helpful when understanding on how and where to use the Action bar, but does this mean the action bar is done all programatically and not in the layout? I guess it's safe to say that I'm a bit "intimidated" by using the action bar because of the different layouts I'll get in different devices... so is there a way to make it "look" like I have an action bar even though it's done purely through layouts? Or is that a bad habit to get into?
I have heard that maybe the compatibility library might work well, but I don't really know what it does and I'm not yet savy enough to understand the android documentation on what it "really" does. If it really is "that" helpful at supporting older devices, why isn't it built/come standard in the SDK?
I know this is a very broad question, but when it boils down to it, I just want to know if there really is a way to make all of my layouts look and work the same, or if it's even worth my time and just have the action bar auto downgrade for old devices.
Just an additional question, I have a Galaxy Nexus and all of the core Google Apps look awesome with the action bar, but is there a way to see these updated apps running on API 11 or lower?
Lastly, if anyone has stumbled upon a great tutorial on any of this please list it. I'm really trying to become knowledgeable on how Android layouts work in all API levels.
Upvotes: 0
Views: 945
Reputation: 10540
If you try to use the ActionBar on a version of Android that is not supported then it will just crash your application. To get around this, you can implement the library ActionBarSherlock which uses the native ActionBar if available, otherwise it creates its own that has the same functionality. You can also check the Android version during runtime and handle it yourself. I would check this link out as it actually has an ActionBar example:
http://developer.android.com/training/basics/supporting-devices/platforms.html#version-codes
Upvotes: 0
Reputation: 850
The easy way if you just need a menu, a title and an icon up there:
actionbar.xml
Implement this in your Activity
@Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.actionbar, menu);
return true;
}
Since this is roughly the same as a menu in older versions, the older versions just lack the ActionBar and have the old menu instead.
If you want the same ActionBar in all your Versions including before API 11, as Geert already suggested:
Upvotes: 1
Reputation: 1006849
I can see that the Action bar was implemented in API level 11, but then what happens when I try to run it on a device lower than 3.0?
By default, the user will get options menus, as before. Or, use ActionBarSherlock to have an action bar working back to Android 2.1, as Mr. Weening noted.
but does this mean the action bar is done all programatically and not in the layout?
It is the same as options menus, using mostly menu resources and Java code.
If it really is "that" helpful at supporting older devices, why isn't it built/come standard in the SDK?
You are welcome to create a time machine and go back in time to add that code to Android devices dating back to 2009. You might be able to find a lightly-used flux capacitor on eBay, though tracking down a Mr. Fusion unit may be somewhat more troublesome. Or, if you're in the UK, see if you can find a used TARDIS dealer.
The rest of us, in the meantime, will use the Android Support package in part for its backports of fragments, GridLayout
, etc., so we can use new capabilities that are not native to older devices, while maintaining API fidelity with the native implementations on newer devices.
Upvotes: 4
Reputation: 1520
To support older version of android with a same looking action bar you could take a look at Action Bar Sherlock
From their website:
The library will automatically use the native action bar when appropriate or will automatically wrap a custom implementation around your layouts. This allows you to easily develop an application with an action bar for every version of Android from 2.x and up
This works fairly well.
If you're a little more adventurous and you'd want more control over it's layout, look and feel, you could always try to implement it yourself. It's not an easy task but the action bar is more of a ux design than an actual component. The code for the compatibility libraries is a good place to start then.
Upvotes: 3