• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Menu not inflating

ShamusVW

Newbie
In my fragment I have the following, however my menu never gets inflated. I have a Toolbar in my layout of the fragment.
In my Logcat I get the following output (filtering on TAG which is "Fragment1"). There is no "Woohoo" output, showing that onCreateOptionsMenu never gets called.
Could someone assist me please? I use a ViewPager on my MainActivity to hold all the fragments in.

08-28 11:20:56.822 9118-9118/? I/Fragment1: 1
08-28 11:20:56.823 9118-9118/? I/Fragment1: 2
08-28 11:20:56.845 9118-9118/? I/Fragment1: 3

Code:
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "1");
    setHasOptionsMenu(true);
    Log.i(TAG, "2");
    View view = inflater.inflate(R.layout.fragment_main_layout, container, false);
    btnNavFragInnovations = (Button) view.findViewById(R.id.button1);
    btnNavFragTraining = (Button) view.findViewById(R.id.button2);
    btnNavFragForms = (Button) view.findViewById(R.id.button3);

    Log.i(TAG, "3");
    btnNavFragInnovations.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity)getActivity()).setmViewPager(1);
        }
    });

    btnNavFragTraining.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity)getActivity()).setmViewPager(2);
        }
    });

    btnNavFragForms.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((MainActivity)getActivity()).setmViewPager(3);
        }
    });

    return view;
}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater menuInflater) {
    Log.i(TAG, "woohoo");
    menuInflater.inflate(R.menu.top_menu, menu);
    super.onCreateOptionsMenu(menu, menuInflater);
}
 
Thank you for your response
At this point, apart from setting up the different fragments, and putting buttons on each fragment to move to a different fragment, I have done no "real" programming in it, i.e. there is no functionality built yet, it just swops between fragments, so I wouldn't even know what might be stopping it being called.
The thing is that it never actually gets called (the onCreateOptionsMenu) since it never logs any response in the Logcat, whether during the onCreateView, or after it has finished.
However, in going to another fragment and then returning to this specific fragment where I am trying to set up a menu, it does keep logging the "1", "2", & "3" from the onCreateView each time, just never the "Woohoo" from the onCreateOptionsMenu. My output shows the Toolbar (set up in the fragment xml file, it just doesn't want to show the hamburger item I am trying to place on it created in the top_menu.xml file.
I am using Windows 10 as my OS, API 23 (Marshmalow), Android 6.0 as targets.
 
I think I might start with the suggestion to put this in onResume()
Code:
invalidateOptionsMenu();

If that doesn't work, you would have to see what the activity lifecycle is for your test device and try tracing through that.
 
I created the onResume method as follows:

Code:
@Override
public void onResume() {
    super.onResume();
    getActivity().invalidateOptionsMenu();
}

Unfortunately it still doesn't run the onCreateOptionsMenu method.

As reference, this is what my MainActivity looks like. As already mentioned, I an using a ViewPager to contain all my fragments.

Code:
package com.example.pdcfactory;

import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;

public class MainActivity extends AppCompatActivity {

//    private static final String TAG = "MainActivity";

    private SectionsStatePageAdapter mSectionsStatePageAdapter;
    private ViewPager mViewPager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSectionsStatePageAdapter = new SectionsStatePageAdapter(getSupportFragmentManager());
        mViewPager = (ViewPager) findViewById(R.id.container);
        setupViewPager(mViewPager);
    }

    private void setupViewPager(ViewPager viewPager) {
        SectionsStatePageAdapter adapter = new SectionsStatePageAdapter(getSupportFragmentManager());
        adapter.addFragment(new FragmentMain(), "FragmentMain");
        adapter.addFragment(new FragmentInnovations(), "FragmentInnovations");
        adapter.addFragment(new FragmentTraining(), "FragmentTraining");
        adapter.addFragment(new FragmentForms(), "FragmentForms");
        viewPager.setAdapter(adapter);
    }

    public void setmViewPager(int fragmentNumber) {
        mViewPager.setCurrentItem(fragmentNumber);
    }

and then my top_menu.xml file:
Code:
<?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/menu_item_settings"
    android:icon="@drawable/ic_menu"
    android:title="test"
    app:showAsAction="always" />
</menu>


have to see what the activity lifecycle is for your test device and try tracing through that

I'm not 100% percent sure what you are referring to here. Are you referring to the Logcat output? Because this is my full output set to verbose, and in the region where it outputs my "1", "2" & "3", I don't see anything obvious (although I'm still learning, just that nothing jumps out at me).

08-30 09:17:40.483 13262-13262/? I/art: Not late-enabling -Xcheck:jni (already on)
08-30 09:17:40.568 13262-13262/com.example.pdcfactory W/System: ClassLoader referenced unknown path: /data/app/com.example.pdcfactory-1/lib/x86
08-30 09:17:40.650 13262-13262/com.example.pdcfactory W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable
08-30 09:17:40.724 13262-13262/com.example.pdcfactory I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
08-30 09:17:40.725 13262-13262/com.example.pdcfactory I/art: Rejecting re-init on previously-failed class java.lang.Class<android.support.v4.view.ViewCompat$OnUnhandledKeyEventListenerWrapper>
08-30 09:17:40.866 13262-13288/com.example.pdcfactory D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-30 09:17:40.899 13262-13262/com.example.pdcfactory I/Fragment1: 1
08-30 09:17:40.899 13262-13262/com.example.pdcfactory I/Fragment1: 2
08-30 09:17:40.923 13262-13262/com.example.pdcfactory I/Fragment1: 3
08-30 09:17:40.977 13262-13288/com.example.pdcfactory I/OpenGLRenderer: Initialized EGL, version 1.4
08-30 09:17:40.977 13262-13288/com.example.pdcfactory W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without...
08-30 09:17:40.987 13262-13288/com.example.pdcfactory D/EGL_emulation: eglCreateContext: 0xae6d4660: maj 3 min 1 rcv 4
08-30 09:17:40.989 13262-13288/com.example.pdcfactory D/EGL_emulation: eglMakeCurrent: 0xae6d4660: ver 3 1 (tinfo 0xae6d2f50)
08-30 09:17:40.990 13262-13288/com.example.pdcfactory E/eglCodecCommon: glUtilsParamSize: unknow param 0x000082da
08-30 09:17:40.991 13262-13288/com.example.pdcfactory E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008cdf
08-30 09:17:40.991 13262-13288/com.example.pdcfactory E/eglCodecCommon: glUtilsParamSize: unknow param 0x00008824
08-30 09:17:41.037 13262-13288/com.example.pdcfactory D/EGL_emulation: eglMakeCurrent: 0xae6d4660: ver 3 1 (tinfo 0xae6d2f50)

I am uploading an image of what my fragment looks like, in the drawn in red square is the item that isn't being shown, it is a "hamburger" i.e. 3 horizontal lines icon.

Thanks again for trying to assist.
 

Attachments

  • Emulator.JPG
    Emulator.JPG
    55.8 KB · Views: 132
As an update, I added some logging into onResume method, and it definitely gets called, but onCreateOptionsMenu still not.

Code:
@Override
public void onResume() {
    Log.i(TAG, "4");
    super.onResume();
    Log.i(TAG, "5");
    getActivity().invalidateOptionsMenu();
    Log.i(TAG, "6");
}

Output in Logcat:

08-30 09:27:50.330 14219-14219/? I/Fragment1: 1
08-30 09:27:50.330 14219-14219/? I/Fragment1: 2
08-30 09:27:50.352 14219-14219/? I/Fragment1: 3
08-30 09:27:50.364 14219-14219/? I/Fragment1: 4
08-30 09:27:50.364 14219-14219/? I/Fragment1: 5
08-30 09:27:50.364 14219-14219/? I/Fragment1: 6
 
That almost looks like you aren't referencing the menu in your activity. Can you share your activity XML?
 
Sorry for the wait. Here is my activity_main.xml
Code:
<?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:id="@+id/constraintLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:background="@color/white">

    <android.support.v4.view.ViewPager
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/container">
    </android.support.v4.view.ViewPager>

</android.support.constraint.ConstraintLayout>

and this is my fragment_main_layout.xml - this is specifically where I'm trying to get the menu, on this fragment:

Code:
<?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">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/logoGreen"
        android:minHeight="?attr/actionBarSize"
        app:layout_constraintTop_toTopOf="parent"
        tools:layout_editor_absoluteX="0dp" />

    <ImageView
        android:id="@+id/imageView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginTop="8dp"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@id/toolbar"
        app:srcCompat="@drawable/logopdc" />

    <ImageView
        android:id="@+id/imageView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:srcCompat="@drawable/logoasc" />

    <android.support.constraint.Guideline
        android:id="@+id/guideline"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintGuide_percent="0.5" />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button1"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="@+id/imageView3"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toEndOf="@+id/imageView3"
        app:layout_constraintTop_toTopOf="@+id/imageView3" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button2"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/button3"
        app:layout_constraintEnd_toEndOf="@+id/button1"
        app:layout_constraintTop_toBottomOf="@+id/button1" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginEnd="24dp"
        android:layout_marginRight="24dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button3"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/button4"
        app:layout_constraintEnd_toEndOf="@+id/button2"
        app:layout_constraintTop_toBottomOf="@+id/button2" />

    <Button
        android:id="@+id/button4"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button4"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/button5"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button3" />

    <Button
        android:id="@+id/button5"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginStart="24dp"
        android:layout_marginLeft="24dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button5"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/button6"
        app:layout_constraintStart_toStartOf="@+id/button6"
        app:layout_constraintTop_toBottomOf="@+id/button4" />

    <Button
        android:id="@+id/button6"
        android:layout_width="wrap_content"
        android:layout_height="43dp"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button6"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toTopOf="@+id/button7"
        app:layout_constraintStart_toStartOf="@+id/button7"
        app:layout_constraintTop_toBottomOf="@+id/button5" />

    <Button
        android:id="@+id/button7"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginLeft="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginRight="8dp"
        android:background="@drawable/button_corner"
        android:gravity="center_horizontal|center_vertical"
        android:hapticFeedbackEnabled="true"
        android:paddingLeft="10dp"
        android:paddingRight="10dp"
        android:text="@string/button7"
        android:textAllCaps="false"
        android:textColor="@color/white"
        app:layout_constraintBottom_toBottomOf="@+id/imageView4"
        app:layout_constraintEnd_toStartOf="@+id/imageView4"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="@+id/imageView4" />

</android.support.constraint.ConstraintLayout>
 
I may be wrong on this, but I think it may be that you're not invoking anything to bring the menu into the activity. From what I can see, you need to have some sort of toolbar/actionbar that you initialize in your activity - see this example https://www.javatpoint.com/android-option-menu-example

So you will need an action bar XML and you'll need to invoke that in your onCreate
 
From what I've seen/read, this is when you would want to inflate it into your Main Activity. However, I am using the ViewPager container in the main activity to swop out my fragments, and hence (I think) I need to inflate them not into the activity, but into the fragments. For this reason setHasOptionsMenu(true) is called in onCreateView in the fragment, and not onCreate in the main activity. AlsoonCreateOptionsMenu has a return of void as opposed to Boolean if inflating into the main activity.
The idea being that the fragments get set up and then the main activity has minimal code only to hold the ViewPager which handles the fragments.

The xml for the menu bar is done in top_menu.xml as below:
Code:
<?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/menu_item_settings"
    android:icon="@drawable/ic_menu"
    android:title="test"
    app:showAsAction="always" />
</menu>

The toolbar is set up in fragment_main_layout.xml as
Code:
<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/logoGreen"
    android:minHeight="?attr/actionBarSize"
    app:layout_constraintTop_toTopOf="parent"
    tools:layout_editor_absoluteX="0dp" />

The little hamburger icon I am trying to display in the menu (saved under drawable) is:
Code:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
</vector>

and in FramentMain.java, the call to inflate this menu (in onCreateOptionsMenu which doesn't get called) is:
Code:
menuInflater.inflate(R.menu.top_menu, menu);
 
@ShamusVW

You need to find yourself a tutorial for a "Navigation Drawer" or a "Fragment Navigation Drawer" which seems to be exactly what you're trying to do. A quick google search should come up with some good tutorials.

Try to avoid using code from an "Overflow Menu" which is the one with the 3 dots. It has similar code, but doesn't seem to be what you want.
 
Thank you for the suggestion, and I will certainly look into this Navigation Drawer as a follow up project. I have seen it spoken about in trying to get this issue of mine sorted.
It isn't the overflow icon that I am trying to put on the menu however, it is the hamburger icon (3 horizontal line) - I want to use this to go into further settings for the fragment.

This is the drawable\ic_menu.xml for the icon
Code:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
        android:width="24dp"
        android:height="24dp"
        android:viewportWidth="24.0"
        android:viewportHeight="24.0">
    <path
        android:fillColor="#FF000000"
        android:pathData="M3,18h18v-2L3,16v2zM3,13h18v-2L3,11v2zM3,6v2h18L21,6L3,6z"/>
</vector>

The tutorial I followed to get the fragments set up in the ViewPager was from
with CodingWithMitch.
I asked a few questions underneath, but unfortunately Mitch only answered to a certain point.

All my fragments switch in and out perfectly, and I am quite happy with how ViewPager does this job for me, I just need an icon to appear on the Toolbar in the main fragment which I can't get right.

At this point I am considering transferring all my fragment code to the main activity code to test if I can get the menu icon to inflate on the main activity (discarding all separate fragments). But it just seems that I am missing something fundamental to get this working, if only I could get the fragment to run the method
onCreateOptionsMenu
 
Hi! Have you resolved this yet? I am struggling terribly with this very problem. After a ton of searching I found one suggestion that actually did allow my overrides to get called as expected. The suggestion was to change the Activity class declaration. There's an incompatibility there. So if you have this: extends Activity, try changing it to this extends AppCompatActivity or vice versa.

This change does allow my overrides to get called. Unfortunately I still do not see my menu!!!! FWIW, I am working with an Activity not a Fragment. Please let me know what this does for you. TIA
 
Back
Top Bottom