user3119647
user3119647

Reputation: 191

Android options menu not displaying

I'm new to Android and I've been trying to add a simple add button as mentioned below

list_menu.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item 
        android:id="@+id/menu_insert"
        android:icon="@android:drawable/ic_menu_add"
        android:title="@string/menu_insert"              
    />     
</menu>

MyActivity.java

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

        return true;  
    }

I read in Dummies series book that ic_menu_add is already there in resources and I don't need to add it, but when I run this code it does not display. I've tried to add a custom icon with same name still there is no button. Can someone help me with it please.

Upvotes: 18

Views: 35137

Answers (11)

Rajesh
Rajesh

Reputation: 664

I was using a theme with NO_ACTION_BAR, the code was generated by Android Studio and I probably picked a template without an action bar. So check your AndroidManifest.xml if the theme has a parent set to NO_ACTION_BAR and change it.

Upvotes: 0

Andre Thiele
Andre Thiele

Reputation: 4191

If it does not show up in your activity, make sure to call setSupportActionBar(R.id.my_toolbar) in your activity's onCreate()

Upvotes: 0

Bonfix Ngetich
Bonfix Ngetich

Reputation: 1237

For me, I had to add the following code to the activity xml:

 <androidx.appcompat.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/colorPrimary"
        android:theme="@style/AppTheme"
        app:popupTheme="@style/Theme.AppCompat.Light.DarkActionBar"
        app:title="@string/app_name"
        app:titleMarginStart="24dp"
        app:titleTextColor="@android:color/white" />

Then to the activity.java: onCreate

Toolbar toolbar = findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

And

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

And fragment onCreate:

 setHasOptionsMenu(true);

Upvotes: 4

harsh kumar
harsh kumar

Reputation: 11

Add

app:showAsAction="always"

to menuitem.

Upvotes: 0

Thirumalvalavan
Thirumalvalavan

Reputation: 2768

I have faced this issue. But in my case, I have added toolbar inside the Framelayout. Top of that I have added one more scroll view with match parent. Now ScrollView took the click control, not toolbar. So if you are using FrameLayout, your toolbar suppose to be the top most view.

Upvotes: 1

Jyubin Patel
Jyubin Patel

Reputation: 1373

Hi hope below code is helpings to you:

    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <item name="colorPrimary">@color/primary</item>
        <item name="colorPrimaryDark">@color/primaryDark</item>
    </style>

Please mention DarkActionBar as your Theme which you used into Android Manifest File.

Upvotes: 0

karthik kumar
karthik kumar

Reputation: 181

Have you missed these lines in your xml file check once

xmlns:tools="http://schemas.android.com/tools"

tools:context=".MainActivity"

if you missed this is what causing problem for displaying option menu

Upvotes: 0

Roel
Roel

Reputation: 3096

If you use a fragment then you need this in onCreate():

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

Upvotes: 17

RaiVikrant
RaiVikrant

Reputation: 539

I was dealing with the same problem.. read some queries and documentation.. Hope this might help you.

Here's my XML file for a menu..

<item
    android:id="@+id/action_send_feedback"
    android:orderInCategory="100"
    android:showAsAction="always"
    android:title="@string/action_send_feedback"/>

<item 
    android:id="@+id/action_share_app"
    android:orderInCategory="100"
    android:showAsAction="ifRoom"
    android:title="@string/action_share_app"
    android:icon="@drawable/ic_action_share" />

<item
    android:id="@+id/action_rate_app"
    android:orderInCategory="100"
    android:showAsAction="never"
    android:title="@string/action_rate_app"/>

JAVA Code goes here..

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

For android phones which have option button (at the bottom of the phone) the menu item which are showAsAction="never" comes when the button is pressed.. or else they will be shown normally on the action bar options menu..

Ref: http://developer.android.com/guide/topics/ui/menus.html#options-menu

Upvotes: 9

MikeHelland
MikeHelland

Reputation: 1159

If you set your TargetSDK in the manifest to 5, the icon will show up.

If you are targeting a newer Android SDK (3.0 and up) the action bar takes over the menu and by default doesn't display icons.

You can try this:

How to show icons in ActionBar overflow menu?

Upvotes: 1

amatellanes
amatellanes

Reputation: 3735

It is not required to call super() method. Try replacing your onCreateOptionsMenu for that:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.list_menu, menu);
    return true;
}

Upvotes: 3

Related Questions