AppCompat - an element that does not appear in the action bar for API levels v8-13

I have one element that I want to always show in the action bar using the AppCompat library. This is just a submenu using the default overflow image.

This does not work for me at API v8-13 levels, and I did everything the developer says, including adding my own namespace.

I use a custom style that has Theme.AppCompat as the parent (below).

This is my first post, so I don’t have enough reputation points to post an activity image, but the item is missing from the action bar and the menu appears at the bottom when I press the menu button.

Any feedback would be greatly appreciated!

AndroidManifest.xml:

<manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.myproject" android:installLocation="auto" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <application android:debuggable="true" android:icon="@drawable/my_app_icon" android:label="@string/app_name" android:theme="@style/CustomActionBarTheme" > <uses-library android:name="com.google.android.maps" /> <activity android:name=".ui.MainActivity" android:label="@string/app_name" android:launchMode="singleTop" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> 

menu.xml

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:MyApp="http://schemas.android.com/apk/res-auto" > <item android:id="@+id/overflow" MyApp:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" MyApp:showAsAction="always" android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" android:showAsAction="always"> <menu> <item android:id="@+id/action_about" android:icon="@drawable/about_icon" android:title="About"/> <item android:id="@+id/action_feedback" android:icon="@drawable/feedback_icon" android:title="Feedback"/> <item android:id="@+id/action_settings" android:icon="@drawable/settings_icon" android:title="@string/action_settings"/> </menu> </item> </menu> 

styles.xml

 <resources> <style name="CustomActionBarTheme" parent="@style/Theme.AppCompat"> <item name="android:actionBarStyle">@style/MyActionBar</item> <item name="actionBarStyle">@style/MyActionBar</item> </style> <style name="MyActionBar" parent="@style/Widget.AppCompat.ActionBar"> <item name="android:titleTextStyle">@style/TitleTextStyle</item> <item name="android:background">@drawable/abc_ab_bottom_solid_dark_holo</item> <item name="titleTextStyle">@style/TitleTextStyle</item> <item name="background">@drawable/abc_ab_bottom_solid_dark_holo</item> </style> <style name="TitleTextStyle" parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"> <item name="android:textStyle">bold</item> </style> </resources> 

MainActivity.java

 package com.myproject; ... public class MainActivity extends BaseActivity { public void onCreate(Bundle savedInstanceState) { super.onCreateEqually(savedInstanceState); ActivityHelper.setLayoutTitle(this, R.layout.main, R.string.main, getSupportActionBar()); } ... } 

BaseActivity.java

 package com.myproject; import android.support.v7.app.ActionBarActivity; ... public class BaseActivity extends ActionBarActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } public boolean onCreateOptionsMenu(Menu menu) { return true; } public boolean onPrepareOptionsMenu(Menu menu) { return MenusHelper.displayMenu(this, menu); } ... } 

MenusHelper.java

 package com.myproject; ... public final class MenusHelper { ... public static boolean displayMenu(BaseActivity currentActivity, Menu menu) { // clear former menus menu.clear(); MenuInflater inflater = new MenuInflater(currentActivity); inflater.inflate(R.menu.menu, menu); return true; } ... } 

ActivityHelper.java

 package com.myproject; import android.support.v7.app.ActionBar; ... public final class ActivityHelper { ... public static void setLayoutTitle(Activity activity, int layoutId, String titleStr, ActionBar actionBar) { activity.setContentView(layoutId); actionBar.setTitle(titleStr); actionBar.setDisplayHomeAsUpEnabled(true); } } 
+7
android android-actionbar appcompat
source share
2 answers

Try putting the code MenusHelper.displayMenu(this, menu); in onCreateOptionsMenu() , not in onPrepareOptionsMenu() , as well as make the necessary changes.

 @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.menu, menu); // In case you have an item MenuItem shareItem = menu.findItem(R.id.menu_share); // To retrieve the Action Provider mActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem); return super.onCreateOptionsMenu(menu); } 

Make sure your menu in XML looks like this:

 <item android:id="@+id/share" android:title="@string/menu_share" yourapp:actionProviderClass="android.support.v7.widget.ShareActionProvider" yourapp:showAsAction="ifRoom|withText"/> 
+11
source share

the problem is this

 android:showAsAction="always" 

which should be

 youpackagename:showAsAction="always" 

really looking at it again, you stated that twice

 android:icon="@drawable/abc_ic_menu_moreoverflow_normal_holo_dark" android:showAsAction="always" 

delete this bit

+7
source share

All Articles