6
votes

There is a new concept in Android:

http://android-developers.blogspot.com/2012/01/say-goodbye-to-menu-button.html

But it isn't clear fro me. I have an application which is support from 1.6 to 4.0. And I want to follow the new concept, but I can't set showAsAction property in the menu xml because:

"No resource identifier found for attribute 'showAsAction' in package 'android'"

It's normal because there is in the doc:

"Note: The android:showAsAction attribute is available only on Android 3.0 (API Level 11) and greater."

How can I set the menu, that under 3.0 is a simple menu but over 3.0 as an ActionBar?

4

4 Answers

5
votes

Per the blog post that you shared:

Summary

Android no longer requires a dedicated Menu button, some devices don’t have one, and you should migrate away from using it.

Set targetSdkVersion to 14, then test your app on Android 4.0.

Add showAsAction="ifRoom" to menu items you’d like to surface in the action bar.

If the ActionBar doesn’t work for your app, you can remove it with Theme.Holo.NoActionBar or Theme.DeviceDefault.NoActionBar.

So, all you really have to do is this:

First, set your SDK min and target version in the AndroidManifest.xml file like this:

<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14" />

Second, add showAsAction="ifRoom" to the menu items in your menu.xml file.

Now run your app in Honeycomb or Ice Cream Sandwich and you should see your menu in the action bar. Your app should still work for the pre Android 3.0 releases.

2
votes

Go to project properties in Eclipse (right-click on project and select Properties), then go to Android section, and change Project Build Target to Android 4.0. This must be done so that the build system recognizes showAsAction.

Don't worry that your project won't be runnable on Android less than 4.0, you can still run it as long as you don't call a class or method available only on 4.0.

0
votes

You can use Build.VERSION to do something different in every versions。 Some like Build.VERSION.SDK_INT<Build.VERSION_CODES.HONEYCOMB

0
votes

You can use some os technology to provide actionbar in apps with API level less then 11.

Consider using ActionBarSherlock. You can just add it as library project to your project and if you won't set the Sherlock theme it won't show action bar(in case you don't want to show it), yet you can mark your menuitems with "showAsAction". On devices with API level higher than 10, action bar with your menuitems will be normally shown.