4

Here is my general.xml file

<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:showAsAction="never"
        android:title="@string/action_settings"/>
    <item 
        android:id="@+id/next" 
        android:title="Next" 
        android:visible="true" 
        android:enabled="true" 
        android:showAsAction="always" 
        android:orderInCategory="1">

    </item>
    <item 
        android:id="@+id/Previous"
        android:title="Previous" 
        android:visible="true" 
        android:enabled="true" 
        android:orderInCategory="2" 
        android:showAsAction="always">

    </item>
    <item android:id="@+id/star" 
        android:icon="@drawable/ic_action_important" 
        android:enabled="true" 
        android:orderInCategory="0" 
        android:showAsAction="always" 
        android:title="Star" 
        android:visible="true">
    </item>

</menu>

Here is my code

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.general, menu);
    getSupportActionBar().setDisplayShowHomeEnabled(false);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setBackgroundDrawable(new ColorDrawable(Color.parseColor("#003f84")));
    return true;
}

Now, my problem is menu items isn't showing in actionbar. Am I doing anything wrong here?

JRomero
  • 4,878
  • 1
  • 27
  • 49
Adnan
  • 8,468
  • 9
  • 29
  • 45

1 Answers1

19

showAsAction should be in a different namespace (yourapp in the sample below).

<menu xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:yourapp="http://schemas.android.com/apk/res-auto" >
    <item android:id="@+id/action_search"
          android:icon="@drawable/ic_action_search"
          android:title="@string/action_search"
          yourapp:showAsAction="ifRoom"  />
    ...
</menu>

youapp is simply a namespace identifier that points to namespace http://schemas.android.com/apk/res-auto, you can change it to anything. The SDK will automatically map that namespace to your package name (see changelog below).

Added support for custom views with custom attributes in libraries. Layouts using custom attributes must use the namespace URI http://schemas.android.com/apk/res-auto instead of the URI that includes the app package name. This URI is replaced with the app specific one at build time.

This is necessary so that it could properly find attributes that are not available in previous OS versions and instead are part of your application's package. Note from documentation:

Notice that the showAsAction attribute above uses a custom namespace defined in the tag. This is necessary when using any XML attributes defined by the support library, because these attributes do not exist in the Android framework on older devices. So you must use your own namespace as a prefix for all attributes defined by the support library.

JRomero
  • 4,878
  • 1
  • 27
  • 49
  • your app that means my app name ? – Adnan Sep 12 '13 at 17:32
  • No, it's your a namespace identifier. It could be called anything as long as it's unique to other namespace identifiers. What is important is the namespace `http://schemas.android.com/apk/res-auto`. – JRomero Sep 12 '13 at 17:36
  • +1 - thanks a lot! The part that solved my problem: "Notice that the showAsAction attribute above uses a custom namespace defined in the tag" I was migrating to ABC from ABS and showAsAction was prepended by "android:" instead of the custom namespace (yourapp: as used above). Once I switched to the correct namespace it finally showed my actions (icons) in the actionbar :) – AgentKnopf May 15 '14 at 06:35