Menu items do not appear in the action bar.

I made a completely new project. I added items to the menu layout file. These items do not appear in the action pane on the right. I remember that a three-dot icon appears that opens a menu.

enter image description here

Here is my Activity

 public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ActionBar actionBar = getActionBar(); actionBar.setBackgroundDrawable(new ColorDrawable(Color.BLUE)); actionBar.show(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } } 

And here is my main.xml

 <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_option1"/> <item android:id="@+id/action_settings34" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_option2"/> <item android:id="@+id/action_settings3" android:orderInCategory="100" android:showAsAction="never" android:title="@string/action_option3"/> </menu> 
+48
android android-actionbar
Aug 02 '13 at 6:22
source share
9 answers

Since you set the showAsAction attribute to never , these menu items will never be displayed as action types. Try the following:

 <menu xmlns:android="http://schemas.android.com/apk/res/android" > <item android:id="@+id/action_settings" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/action_option1"/> <item android:id="@+id/action_settings34" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/action_option2"/> <item android:id="@+id/action_settings3" android:orderInCategory="100" android:showAsAction="ifRoom|withText" android:title="@string/action_option3"/> </menu> 
+39
Aug 02 '13 at 6:38 on
source share
— -

Context

There seems to be a misunderstanding about showAsAction this menu setting. The information I find in many stackoverflow answers always seems incomplete. So here is an attempt to change that.

Three points | overflow | options menu

It is not possible to control whether the menu for dragging three points is displayed on your action screen or not in the code.

Well, you can make it not show without having an options menu. And frankly, you can hack this, more information in How to get to use the overflow menu on devices using the menu button

Points that are displayed or independent of the device you are testing,. Devices with a menu button DO NOT show the three dots icon on the action bar, as users can use the menu button on the device.

options menu button on device

Devices that do not have this option menu button (for example, communication devices starting with Galaxy) do not have an alternative to launch this option menu. Running the same application on these devices displays three point overflow options in the action menu.

actionbar showing options menu

What can you control

There is a showAsAction parameter in the xml file of the menu file, which you can use to to control how this single menu is displayed in the action bar.

According to the official resource menu resources , the following options:

 android:showAsAction=["ifRoom" | "never" | "withText" | "always" | "collapseActionView"] 

The ifRoom flag ifRoom shows an option if there is enough space. This should be your first choice in most cases. This, however, condenses the title in the action bar (see. Vertical screenshot).

The never flag will always have this option in the overflow menu and will never be directly in the action bar.

Using the always flag is discouraged, as it will cause the parameter to always be displayed in acitonbar. Even when working on a very small screen. This can make the title completely invisible.

As you could now, the options menu can have both the android:title property and the android:icon property. You can use the withText flag with always or ifRoom (use the channel | between them) to make the text of the element appear next to the icon when rendering to the action bar.

collapseActionView is only supported with API level 14 , so let me ignore this.

Screenshot showing the following code snippet in action on a device in portrait mode:

collapsed title and mostly icons on vertical orientation

And another screenshot, in which there is more space on the action bar due to landscape mode:

landscape mode actionbar rendered

Android Documentation

The best document for developing this action bar would be an action guide . If you are looking more for development guidelines, follow this link . The link to the resource was indicated earlier. For the menu as a whole, check this out .

Code snippet

The code is for reference only, as this part seems correct in most of the answers. A more complete example can be found at https://github.com/AndroidExamples/fragment-navigation

The contents of the file /res/menu/main.xml :

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" tools:context="be.hcpl.android.example.MainActivity" > <!-- an option that never appears as a fixed text/icon in the actionbar --> <item android:id="@+id/action_one" android:title="@string/action_one" android:orderInCategory="100" app:showAsAction="never" /> <!-- another option that only appears if there is enough room --> <item android:id="@+id/action_two" android:title="@string/action_two" android:orderInCategory="200" app:showAsAction="ifRoom" /> <!-- a last option that always appears --> <item android:id="@+id/action_three" android:title="@string/action_three" android:orderInCategory="300" app:showAsAction="always" /> <!-- an option to show with both icon and text --> <item android:id="@+id/action_four" android:icon="@android:drawable/ic_menu_directions" android:title="@string/action_four" android:orderInCategory="400" app:showAsAction="withText|always" /> <!-- an option to always show as an icon only --> <item android:id="@+id/action_five" android:icon="@android:drawable/ic_menu_info_details" android:title="@string/action_five" android:orderInCategory="500" app:showAsAction="always" /> </menu> 

Some related answers

  • Action buttons not showing in action bar?
  • Action bar not shown with AppCompat
  • Android - want to show 3-point menu on ICS
+114
Apr 15 '14 at 9:47
source share

I think you want to show the overflow menu icon in the upper right and want your menu to open from there. Try something like the following to get it there:

Inside onCreate() do:

  try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if(menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception ex) { // Ignore } 

This will cause the application to display the overflow menu. The menu button will still work, but it will open the menu in the upper right corner. Adapted from this answer at How to force the overflow menu on devices using the menu button . It may be a kind of "hack", but it works.

Edit:

Based on helpful comments from @gunar, I ask you not to use this method. Below are the reasons :

  • The sHasPermanentMenuKey private field may be reorganized in future releases. Then your application will stop working.

  • Also, if you forcefully move action elements to the action bar because you are used to them, you will end up confusing users who are used to how applications work on their device. Those who have a menu hardware key will be used to have options shown with a menu hardware key. If you intentionally show them the three points of the overflow menu, this may confuse them.

So, let all applications be the same in this sense. These devices without a hardware key for menu items will display them automatically.

+9
Aug 02 '13 at 6:32 on
source share

OR you could create a structure like:

 <?xml version="1.0" encoding="utf-8"?> <menu > <item android:id="@+id/menu_main" android:icon="@drawable/icon_menu" android:showAsAction="always" android:title="@string/menu_A" android:visible="true"> <menu> <item android:id="@+id/menu_settings1" android:showAsAction="never" android:title="@string/menu_A1"/> <item android:id="@+id/menu_settings2" android:showAsAction="never" android:title="@string/menu_A2"/> <item android:id="@+id/menu_settings3" android:showAsAction="never" android:title="@string/menu_A3"/> </menu> </item> </menu> 

In the place where you can create a new icon with a triangular icon icon.

+6
Aug 21 '14 at 19:29
source share

After searching and reading a large number of answers, I found that there is one more trifle that you should pay attention to.

At least that was my problem, and I did not find the answer to anything above. In your .XML file. Your android:showAsAction= should not be the android package form, it should be from the app , as in the example below:

 app:showAsAction= 

Remember to import the app package.

+1
Jul 26 '16 at 14:15
source share

Add

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); 

in the onCreate method.

+1
Apr 02 '17 at 7:34 on
source share

You need to call this function in the onCreate function of the activity.

 RequestWindowFeature (Window.FEATURE_ACTION_BAR); 
0
Nov 15 '15 at 18:38
source share

all answers above explained this problem well

but can we override this restriction and have an overflow menu item ?!

yes we can hack

  private void makeActionOverflowMenuShown() { //devices with hardware menu button (eg Samsung Note) don't show action overflow menu try { ViewConfiguration config = ViewConfiguration.get(this); Field menuKeyField = ViewConfiguration.class.getDeclaredField("sHasPermanentMenuKey"); if (menuKeyField != null) { menuKeyField.setAccessible(true); menuKeyField.setBoolean(config, false); } } catch (Exception e) { Log.d(TAG, e.getLocalizedMessage()); } } 

and inside onCreate name it

links: stack overflow

0
May 6 '16 at
source share

add your own namespace like this to show AsAction and actionViewClass

 <menu xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <item android:id="@+id/search" android:title="@string/search" android:icon="@drawable/ic_search" app:showAsAction="collapseActionView|ifRoom" app:actionViewClass="android.widget.SearchView" /> 
0
Sep 17 '17 at 10:46 on
source share



All Articles