showAsAction=“ifRoom” doesn't show the item even when there is plenty of room

爷,独闯天下 提交于 2019-11-29 22:49:17
josagyemang

I hope I am not too late in coming to the party.

It is really not a big fat lie but a small oversight.

The showAsAction attribute must be defined using a different namespace "http://schemas.android.com/apk/res-auto"

You should therefore in your top menu tag define a namespace as follows xmlns:app="http://schemas.android.com/apk/res-auto"

and then use that to define your showAsAction attribute like so app:showAsAction="ifRoom"

That should fix it

It's because there is specified maximum number of items that should go to actionbar and it seems to be 4. Of course you can force them to appear by setting showAsAction: always but regarding to google API guides:

If you believe that more than four of your menu items can be justified as action items, then you should carefully consider their relative level of importance and try to set no more than four as action items (and do so using the "ifRoom" value to allow the system to put some back in the overflow menu when space is limited on smaller screens). Even if space is available on a wide screen, you should not create a long stream of action items that clutter the UI and appear like a desktop toolbar, so keep the number of action items to a minimum.

Additionally, the following actions should never appear as action items: Settings, Help, Feedback, or similar. Always keep them in the overflow menu.

To complement the answer from Michal Z.: The Android Design Guide Page Patterns->Actionbar says the following in the chapter "Action Buttons":

http://developer.android.com/design/patterns/actionbar.html

How many actions will fit in the main action bar? Action bar capacity is controlled by the following rules:

  • Action buttons in the main action bar may not occupy more than 50% of the bar's width.
  • Action buttons on bottom action bars can use the entire width.
  • The screen width in density-independent pixels (dp) determine the number of items that will fit in the main action bar:
    • smaller than 360 dp = 2 icons
    • 360-499 dp = 3 icons
    • 500-599 dp = 4 icons
    • 600 dp and larger = 5 icons
Rahul_Pawar

use app:showAsAction="always" instead of android:showAsAction="always"

Ravi Karki

Use

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

ActivityBar strives not to exceed some amount of visible elements. And that limit is lower than ActivityBar can contain indeed. The limit is set in android.support.v7.view.ActionBarPolicy class:

`/**
     * Returns the maximum number of action buttons that should be permitted within an action
     * bar/action mode. This will be used to determine how many showAsAction="ifRoom" items can fit.
     * "always" items can override this.
     */
    public int getMaxActionButtons() {
        final Resources res = mContext.getResources();
        final int widthDp = ConfigurationHelper.getScreenWidthDp(res);
        final int heightDp = ConfigurationHelper.getScreenHeightDp(res);
        final int smallest = ConfigurationHelper.getSmallestScreenWidthDp(res);

    if (smallest > 600 || widthDp > 600 || (widthDp > 960 && heightDp > 720)
            || (widthDp > 720 && heightDp > 960)) {
        // For values-w600dp, values-sw600dp and values-xlarge.
        return 5;
    } else if (widthDp >= 500 || (widthDp > 640 && heightDp > 480)
            || (widthDp > 480 && heightDp > 640)) {
        // For values-w500dp and values-large.
        return 4;
    } else if (widthDp >= 360) {
        // For values-w360dp.
        return 3;
    } else {
        return 2;
    }
}`

As you can see the limit is between 2 and 5, and it depends on the screen width.

I haven't been able to find a way to change this behavior. So if you want to exceed the limit, you should use showAsAction="always" or create your own view for ActionBar.

Creator

Use this below code it will always work

<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=".MainActivity">
    <item 
        android:id="@+id/menu_share" 
        android:title="Share"
        android:orderInCategory="100" 
        app:showAsAction="ifRoom"
        android:actionProviderClass="android.widget.ShareActionProvider"/>
</menu>
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!