Android menu and action bar conversion

androidandroid-actionbarmenu

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?

Best Answer

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.