Naruto
Naruto

Reputation: 9634

Create an option menu

I'm able to create an option menu in Android through XML file, but I would like to add the second menu programmatically just before the menu is added from XML.

In example: the code below adds a menu item search.

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

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>

</menu>

If I add a new menu item programatically, like

   menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.jingle_bell).
                setShowAsAction(MenuItem.SHOW_AS_ACTION_ALWAYS);

It is appearing after the Search menu item in Actionbar, but I need it before the search menu item.

What changes do I need to make?

Upvotes: 0

Views: 106

Answers (3)

Amir Mohsenian
Amir Mohsenian

Reputation: 94

Try below example

in XML :

<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
    android:id="@+id/action_guide"
    android:title="title"
    android:icon="@drawable/ic_title"
    app:showAsAction="always"
     />

then copy below code to your activity :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main_actions, menu);
    return true;
}

Upvotes: 0

jakir hussain
jakir hussain

Reputation: 316

Try this :

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.yourmenu, menu);
    menu.add(0,Menu.FIRST, Menu.NONE,"Gift box").setIcon(R.drawable.ic_refresh).
            setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

Update:

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

<item
    android:id="@+id/action_search"
    android:icon="@drawable/search"
    android:orderInCategory="200"
    app:showAsAction="ifRoom|collapseActionView"
    app:actionViewClass="android.support.v7.widget.SearchView"
    android:title="Search"/>

</menu>

Upvotes: 1

Aditya
Aditya

Reputation: 3673

Just set order for your item as,

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.yourmenu, menu);
    menu.add(Menu.NONE, Menu.FIRST, 1,"Gift box").setIcon(R.drawable.ic_refresh).
            setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
    return true;
}

Third parameter is order for above menu item. Now in your menu.xml set order for your item as,

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

    <item
        android:id="@+id/action_search"
        android:icon="@drawable/search"
        android:orderInCategory="2"
        app:showAsAction="ifRoom|collapseActionView"
        app:actionViewClass="android.support.v7.widget.SearchView"
        android:title="Search"/>

</menu>

Upvotes: 0

Related Questions