oncreateoptionMenu in android toolbar

127 Views Asked by At

Hello friends i want to create option menu like below image

enter image description here

so i create below menu.xml file:

<?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/live_cart"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:icon="@drawable/cart"
        android:title=""/>
    <item
    android:id="@+id/overflow"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:icon="@drawable/ic_menu_overflow"
    android:title="">
    <menu>
        <item android:id="@+id/action_dasbboard"

          android:title="Logout"
          android:showAsAction="never"
           />
        <item android:id="@+id/export_data"

            android:title="My accout"
            android:showAsAction="never"
            />
    <item android:id="@+id/action_help"

          android:title="Wishlist"
          android:showAsAction="never"
           />
    <item android:id="@+id/action_feedback"

          android:title="Track order"
          android:showAsAction="never"
           />
     <item android:id="@+id/about"

          android:title="Help"
          android:showAsAction="never"
           />
        <item
            android:id="@+id/terms"
             android:title="Legal"
            app:showAsAction="never"/>

    </menu>
</item>


</menu>

When i run above code i will come like below

enter image description here

it is coming only one option so how can i solve this problem any idea?

4

There are 4 best solutions below

0
On

insert

<item
android:id="@+id/live_cart"
android:orderInCategory="100"
app:showAsAction="always"
android:icon="@drawable/cart"
android:title=""/>

try this

0
On

Please take a look:

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

    <item
        android:id="@+id/live_cart"
        android:icon="@drawable/ic_merge"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title=""/>

    <item
        android:id="@+id/action_dasbboard"

        android:showAsAction="never"
        android:title="Logout"
        />
    <item
        android:id="@+id/export_data"

        android:showAsAction="never"
        android:title="My accout"
        />
    <item
        android:id="@+id/action_help"

        android:showAsAction="never"
        android:title="Wishlist"
        />
    <item
        android:id="@+id/action_feedback"

        android:showAsAction="never"
        android:title="Track order"
        />
    <item
        android:id="@+id/about"

        android:showAsAction="never"
        android:title="Help"
        />
    <item
        android:id="@+id/terms"
        android:title="Legal"
        app:showAsAction="never"/>


</menu>
0
On

Remove the second menu and then it should work:

    <?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/live_cart"
        android:icon="@drawable/cart"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="" />
    <item
        android:id="@+id/overflow"
        android:icon="@drawable/ic_menu_overflow"
        android:orderInCategory="100"
        android:showAsAction="always"
        android:title="" />

    <item
        android:id="@+id/action_dasbboard"
        android:showAsAction="never"
        android:title="Logout" />
    <item
        android:id="@+id/export_data"
        android:showAsAction="never"
        android:title="My accout" />
    <item
        android:id="@+id/action_help"

        android:showAsAction="never"
        android:title="Wishlist" />
    <item
        android:id="@+id/action_feedback"
        android:showAsAction="never"
        android:title="Track order" />
    <item
        android:id="@+id/about"

        android:showAsAction="never"
        android:title="Help" />
    <item
        android:id="@+id/terms"
        android:title="Legal"
        app:showAsAction="never" />

</menu>
0
On

inflate this xml in your activity

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}