Shlomi
Shlomi

Reputation: 363

Change the android options menu layout

I'm using android options menu defined in xml under the \menu folder. The menu have 5 items in it, and they are shown in the layout on two lines - the top line contains 2 items and the bottom line contains the other 3 items.

Is there a way to configure the menu so the top line will contain 3 items and the bottom line will contain the other 2?

Thanks!

Upvotes: 1

Views: 5383

Answers (2)

Naveed Ahmad
Naveed Ahmad

Reputation: 6747

We can use different layouts for the options menu for example :-

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/menuRefresh"
     android:icon="@drawable/ic_menu_refresh"
     android:title="Refresh"
      android:showAsAction = "always"/>
<item android:id="@+id/menuAbout"
     android:icon="@drawable/ic_menu_info_details"
     android:title="About"
     android:showAsAction = "always"/>
</menu>

You can use "never" "withtext" "ifRoom" "always" "collapseActionView" or you can read out full details here

Upvotes: 0

androidEnthusiast
androidEnthusiast

Reputation: 1160

I think that if you put menu items in one group it will try to keep them together. To control order of menu items is better when you create Menu in your activity, not using XML. In your activity you can get menu items in group something like this:

@Override
        public boolean onCreateOptionsMenu(Menu menu)
        {
        int group1Id = 1; 

        int Id1 = Menu.FIRST;
        int Id2 = Menu.FIRST +1;
        int Id3 = Menu.FIRST +2;

             super.onCreateOptionsMenu(menu);

                menu.add(group1Id, Id1, 1, "Option 1");
                menu.add(group1Id, Id2,  2, "Option 2");
                menu.add(group1Id, Id3,  3, "Option 3");
                      return true;
            }      

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            super.onOptionsItemSelected(item);

            switch (item.getItemId()) {
            case 1:        //for option 1


                return true;
            case 2:      //for option 2

                return true;
            case 3:        //for option 3


                return true;      
                   default:
                return false;
            }

Parameters: groupId The group identifier that this item should be part of.

itemId Unique item ID. Use NONE if you do not need a unique ID.

order The order for the item. Use NONE if you do not care about the order.

title The text to display for the item.

Returns: The newly added menu item.

Upvotes: 0

Related Questions