18

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.

Melquiades
  • 8,496
  • 1
  • 31
  • 46
user3119647
  • 191
  • 1
  • 1
  • 6

10 Answers10

17

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

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }
Roel
  • 3,089
  • 2
  • 30
  • 34
9

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

RaiVikrant
  • 529
  • 6
  • 15
3

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;
}
amatellanes
  • 3,645
  • 2
  • 17
  • 19
3

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);
Bonfix Ngetich
  • 1,087
  • 11
  • 15
1

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?

Community
  • 1
  • 1
MikeHelland
  • 1,151
  • 1
  • 7
  • 17
  • yes that worked but i dont want to display it in ActionBar it should be at the bottom of screen please guide about it thank u . – user3119647 Jan 19 '14 at 08:53
  • I would suggest first using the standard UI. Your second best option may be targeting a lower sdk in your manifest (like 9), and your least best option creating your own menu with views in your activity's layout that are made visible and invisible on when the menu button is pressed (http://stackoverflow.com/questions/4239880/detecting-physical-menu-key-press-in-android) The last is a bad idea because if the device doesn't have a menu button (which some don't), you're out of luck. – MikeHelland Jan 19 '14 at 09:03
1

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.

Thirumalvalavan
  • 2,660
  • 1
  • 30
  • 32
0

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

karthik kumar
  • 181
  • 1
  • 5
0

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.

Jyubin Patel
  • 1,373
  • 7
  • 17
0

Add

app:showAsAction="always"

to menuitem.

Milo
  • 3,365
  • 9
  • 30
  • 44
0

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

Andre Thiele
  • 3,202
  • 3
  • 20
  • 43