onCreateOptionsMenu is never called
Asked Answered
H

12

74

I am having some trouble getting an options menu working in Android. I have built apps before, and they all worked fine, but now the menu just doesn't pop up.

The code:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    super.onCreateOptionsMenu(menu);

    getMenuInflater().inflate(R.menu.activity_video, menu);
    return true;
}

the whole method is never even called (checked by setting a breakpoint). The activity is super-simple, it just has a VideoView in it, with an OnTouchListener set.

I am using Android 4.0.4 on a Samsung Galaxy 10.1, API level 15, minSDK 15. Am I missing something?

Hatchett answered 7/11, 2012 at 9:43 Comment(7)
What theme are you using? You won't get a menu if you use fullscreen...Hedelman
NoTitleBar: android:theme="@android:style/Theme.Black.NoTitleBar", I have used it with success on other apps.Hatchett
humor me and try to set it to something like holo etc.Hedelman
Right. In holo it does get called.Hatchett
This might be relevant: https://mcmap.net/q/270814/-creating-options-menu-in-android/969325Hedelman
use menuInflater.inflate(R.menu.feedback_menu, toolbar.menu) instead of setSupportActionBarCalifornia
In my case I had been wrongly using FragmentActivity to extend the underlying activity. Changing that to AppCompatActivity fixed the issue (onCreateOptionsMenu got called).Lattermost
H
35

If the phone you test on has a menu button onCreateOptionsMenu wont't be called on start with the theme:

android:theme="@android:style/Theme.Black.NoTitleBar"

But when you click the menu button the onCreateOptionsMenu will be called. I don't know what happens on phones without hardware buttons...

Hedelman answered 7/11, 2012 at 10:5 Comment(2)
without hardware buttons, the menu just is just not rendered at allHatchett
@BartFriederichs ouch, but good to know. I was hoping there was something =/Hedelman
W
79

In the latest versions of Android when using the compat library for toolbar, is very common that this happens, in order to get the menu items to display in the toolbar you must do the following:

mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
Wallow answered 27/7, 2015 at 5:19 Comment(0)
F
63

Call setHasOptionsMenu function from onCreate first. The onCreateOptionsMenu will be automatically called.

Try this:

setHasOptionsMenu(true)
Foretoken answered 18/10, 2013 at 3:23 Comment(2)
This is only applicable to fragments. For a lot of other tips and tricks related to the options menu and fragments see here: #6280071Nel
this did not work for me when using fragments. - 27.12.2017Halophyte
H
35

If the phone you test on has a menu button onCreateOptionsMenu wont't be called on start with the theme:

android:theme="@android:style/Theme.Black.NoTitleBar"

But when you click the menu button the onCreateOptionsMenu will be called. I don't know what happens on phones without hardware buttons...

Hedelman answered 7/11, 2012 at 10:5 Comment(2)
without hardware buttons, the menu just is just not rendered at allHatchett
@BartFriederichs ouch, but good to know. I was hoping there was something =/Hedelman
G
29

In the method: Fragment#onCreateView(...) you should put:

setHasOptionsMenu(true);

Then your method will be called.

Godin answered 30/8, 2015 at 7:58 Comment(3)
This is not a method in my activity.Armpit
This method is called in onCreateView of Fragment Michael Fulton. It work well...Uncontrollable
I previously put it in onCreate like it's suggested even here, but the magic trick was to move it to onCreateViewDin
A
17

That is because the activity does not have the toolbar.

There are 2 steps in order to do it.

First, you need to add the toolbar in your activity.xml which is in res/layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"">

    <!-- add this part-->
    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

Second, let your activity append it

in JAVA

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

or in Kotlin

val toolbar = findViewById<Toolbar>(R.id.toolbar)
setSupportActionBar(toolbar)
Accustom answered 21/3, 2019 at 11:27 Comment(0)
S
4

I was having the same problem (menu not showing up, onCreateOptionsMenu not being called).

If you are calling this within a fragment, you need to override public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) not public boolean onCreateOptionsMenu(Menu menu). Fragments do not use the latter, so they will never even call it.

Activity menu

Fragment menu

Selimah answered 4/12, 2013 at 21:53 Comment(0)
B
4

This happens because you have defined a Theme in your styles.xml that has something like:

<style name="AppTheme" parent="Theme.AppCompat.Light.**NoActionBar**">

You can manually create a toolbar, and then yo have to add it to your Activity.

The xml for the toolbar view could be something like:

<?xml version="1.0" encoding="utf-8"?>
<androidx.appcompat.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="@style/ThemeOverlay.AppCompat.Light">
</androidx.appcompat.widget.Toolbar>

Then, in the Activity, you have to "display" it :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
    actionBar.setDisplayShowTitleEnabled(false);
}

And now, you can create the menu with the known onCreateOptionsMenu

Bloch answered 27/2, 2021 at 23:30 Comment(1)
I thought that actionBar.setDisplayShowTitleEnabled(false); would cause the title on my toolbar to disappear, but it didn't. Plus, this solved my problem!Trochilus
T
3

I had the same issue. My problem was solved by inheritance of a different activity class.

So I had been doing:

public class WelcomeActivity extends Activity

but changed this to:

public class WelcomeActivity extends AppCompatActivity

This way I was saying that say an action bar can be added to your activity.

Thinkable answered 15/9, 2016 at 11:29 Comment(0)
H
2

Maybe you also have overrode onKeyDown method and made it always return true. Returning true means that keyEvent will be prevented from being propagated further. See code below:

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
        /*
          handling event
         */
        return true; //onCreateOptionsMenu won't be invoked.
}
Highhanded answered 16/10, 2013 at 8:17 Comment(0)
S
2

I had a similar issue, but a different solution I am sharing with the community (as it took me one hour to understand what was happening):

    abstract class BaseActivity : AppCompatActivity{
      override fun onCreate(savedInstanceState: Bundle?) {
      setSupportActionBar(my_toolbar)
      }
    }
    class MyActivity : BaseActivity{
        // TODO : some good stuff
    }

where my_toolbar is an object created in my xml file through dataBinding.

The issue looks the same, no toolbar appear, no call to onCreateOptionsMenu.

My solution was to promote this code to the child class and not to that basic, as my_toolbar is only initialized as the child class is built.

Subclass answered 29/8, 2018 at 22:17 Comment(0)
H
0

If above answer doesn't work, make sure that you are using the same id of toolbar.

layout_app_bar.xml

<androidx.appcompat.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

activiy_main.xml

<include
    android:id="@+id/app_bar"
    layout="@layout/layout_app_bar" />

in java file:

don't call:

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

please call:

Toolbar tb = findViewById(R.id.app_bar);
Headway answered 7/9, 2019 at 11:59 Comment(0)
L
-3

Try This It works for me:---

@Override
public boolean onCreateOptionsMenu(Menu menu)
{
    super.onCreateOptionsMenu(menu);
    getMenuInflater().inflate(R.menu.home_page_menu, menu);
    return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_delete:
        Toast.makeText(this, "You pressed the Delete!",
                 Toast.LENGTH_LONG).show();
        break;


      case R.id.menu_setting:
     Intent intent=new Intent(HomePage.this,Setting.class);
     startActivity(intent);
     finish();
     Toast.makeText(this, "You pressed the Setting!",
     Toast.LENGTH_LONG).show(); break;


    }

    return super.onOptionsItemSelected(item);
}
Luo answered 7/11, 2012 at 9:54 Comment(1)
The OP is saying the onCreateOptionsMenu is never called. Altering the contents of the method will not help.Armpit

© 2022 - 2024 — McMap. All rights reserved.