What is orderInCategory in ActionBar menu item & why it is use for..?

AndroidAndroid ActionbarAndroid Menu

Android Problem Overview


Im working on action menu item and its over flow item this is my main_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<item
    android:id="@+id/menu_search"
    android:icon="@drawable/search"
    android:title="@string/start_new_project"
    app:showAsAction="always" />
<item
    android:id="@+id/menu_dts_overflow"
    android:icon="@drawable/ic_action_overflow_round"
    android:orderInCategory="11111" 
    android:title="Service"
    app:showAsAction="always">
    <menu>
        <item
            android:id="@+id/menu_newProject"
            android:icon="@drawable/newproject"
            android:title="@string/start_new_project"
            app:showAsAction="never" />


        <item
            android:id="@+id/menu_help"
            android:icon="@drawable/help"
            android:title="Service Tasks"
            app:showAsAction="never" />


        <item
            android:id="@+id/menu_signOut"
            android:icon="@drawable/signout"
            android:title="@string/menusignout"
            app:showAsAction="never" />

    </menu>
</item>

I tried to construct a search item and a overflow item which you can see in the above code. I'm new to Action bar menu items so i tried to Google it and was able to make it work as I need.

In this I have to know one more thing.

1. What is orderInCategory with some numbers and what for it is used..?

Android Solutions


Solution 1 - Android

android:orderInCategory is an integer attribute that dictates the order in which the menu items will appear within the menu when it is displayed.

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

    <item
        android:id="@+id/menu_item_first"
        android:orderInCategory="1"
        android:showAsAction="never"
        android:title="@string/string_one"/>

    <item
        android:id="@+id/menu_item_second"
        android:orderInCategory="2"
        android:showAsAction="never"
        android:title="@string/string_two"/>

</menu>

-

> Menu items in ToolBar are arranged from left to right (or > start to end in RTL mode) in the ascending order (i.e. 1,2,3 -> > left to right).

horizontal menu items

> Menu Items in Overflow menu are arranged from top to bottom in > the ascending order (i.e. 1,2,3 -> top to bottom).

vertical overflow menu

Solution 2 - Android

android:orderInCategory is actually useful in two ways.

1. For menu items in ActionBar. Items will appear from left to right in ActionBar depending on the ascending order.

2. For menu items in overflow menu. Overflow menu items will be displayed from top to bottom depending upon the ascending order you have specified.

Solution 3 - Android

android:orderInCategory Higher value, lower priority.

I have a Activity and a Fragment in it, both of them have option menu, and the item numbers are 1 and 3.

If I set android:orderInCategory=0, the activity menu are above the fragment menu, same effect before I set the value.

But if I set android:orderInCategory=1,the activity menu are below the fragment menu, and that is what I want.(I also test android:orderInCategory=5 tested too, still the same effect.)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionSaravanaRajaView Question on Stackoverflow
Solution 1 - AndroidY.SView Answer on Stackoverflow
Solution 2 - AndroidApurvaView Answer on Stackoverflow
Solution 3 - AndroidW.ManView Answer on Stackoverflow