Skip to content Skip to sidebar Skip to footer

Options Menu Not Displaying On Actionbar

I wanted to display Menu in ActionBar using ActionBarActivity. I am getting options menu while clicking options button instead of displaying in the top of action bar. I need the op

Solution 1:

Try it:

Create XML file on "../res/menu/menu_main.xml"

manu_main.xml

<menuxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/menu_settings"android:showAsAction="never"android:icon="@android:drawable/ic_menu_settings"android:title="@string/menu_settings"/></menu>

The option "never" on "showAsAction", important, if you put that the option always show on ActionBar.

Then now we must asociate this option on the MainActivity.java (class whatever you want).

MainActivity.java

publicclassMainActivityextendsActivity {

    ...

    @OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        returntrue;
    }
}

Now that we define the elements of our action bar is knowing how to respond to keystrokes made ​​by the user about them.

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_settings:
            Log.i("ActionBar", "Settings!");;
            //open Activity,Fragments or other actionreturntrue;
        default:
            returnsuper.onOptionsItemSelected(item);
    }
}

If you have morer options on your menu only add item on XML menu_main.xml and on the principal class add other "case".

For Example:

MainActivity.java

@OverridepublicbooleanonOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {

        case R.id.menu_save:
            Log.i("ActionBar", "Save!");;
            //open Activity,Fragments or other actionreturntrue;
        case R.id.menu_settings:
            Log.i("ActionBar", "Settings!");;
            //open Activity,Fragments or other actionreturntrue;
        default:
            returnsuper.onOptionsItemSelected(item);
    }
}

menu_main.xml

<menuxmlns:android="http://schemas.android.com/apk/res/android" ><itemandroid:id="@+id/menu_settings"android:showAsAction="never"android:icon="@android:drawable/ic_menu_settings"android:title="@string/menu_settings"/><itemandroid:id="@+id/menu_save"android:showAsAction="never"android:icon="@android:drawable/ic_menu_save"android:title="@string/menu_save"/></menu>

Solution 2:

It is possible that this is because your device has an options button that would open the menu, so Android don't draw the launcher icon.

http://blog.vogella.com/2013/08/06/android-always-show-the-overflow-menu-even-if-the-phone-as-a-menu/

Solution 3:

Try with return super.onCreateOptionsMenu(menu); instead of return true in your onCreateOptionsMenu method according to below android document...

http://developer.android.com/guide/topics/ui/actionbar.html#ActionItems

As per above android document...

The ActionBar APIs were first added in Android 3.0 (API level 11) but they are also available in the Support Library for compatibility with Android 2.1 (API level 7) and above. So, If you want ActionBar in android 2.3 then you have to use Android Support Library.

Edit:

Your menu XML would need to look like this:

<?xml version="1.0" encoding="utf-8"?><menuxmlns:android="http://schemas.android.com/apk/res/android"xmlns:yourapp="http://schemas.android.com/apk/res-auto"
><itemandroid:id="@+id/item_menu_ok"android:icon="@drawable/ic_action_ok"android:title="@string/ok"yourapp:showAsAction="always"/><itemandroid:id="@+id/item_menu_cancel"android:icon="@drawable/ic_action_cancel"android:title="@string/cancel"yourapp:showAsAction="always"/></menu>

Replace yourapp with your app name or any namespace.

Solution 4:

I'm not 100% sure, but it looks like your menu XML file should have a menu in it. In the menu, add the items. Like this:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_menu" >

    <item...

    <item...

</menu>

Perhaps this works.

There are also a few other things to remember in general. Android decides by itself what to show in the ActionBar (depending on what fits there and other parameters). What does not fit in the ActionBar goes into the overflow.

Solution 5:

Do this in your oncreate()

ActionBaractionBar= getActionBar();
actionBar.show();

This should do what you want. Action Bar

Post a Comment for "Options Menu Not Displaying On Actionbar"