Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionBarCompat menu item is not showing [duplicate]

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?

like image 452
Adnan Avatar asked Dec 01 '22 20:12

Adnan


1 Answers

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.

like image 128
JRomero Avatar answered Dec 09 '22 18:12

JRomero