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

Menu for fragment when using SectionsStatePageAdapter

ShamusVW

Newbie
I followed a video
tutorial on creating fragments making use of a ViewPager & SectionsStatePageAdapter. (By ordering comments on this YouTube video by NEW first, you can see my comments to the developer under the user 'Shaun')
Subsequently I have tried adding a menu into my main fragment.

In my fragment_main_layout.xml I have the following code (along with any buttons definitions, etc.) These all show up in the Design view of the fragment, as well as when the app is running. It is however just a plain toolbar, and I want to add items to the toolbar, like a menu icon. I can't get this menu icon to show though.

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" />

I also created a menu called top_menu.xml under res\menu folder, adding into the definition the icon, title, id & showAsAction. When I view this in Design view, it shows a toolbar with the icon, as well as the name of the app.

Code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:myapp="http://schemas.android.com/apk/res-auto">

    <item android:id="@+id/menu_item_settings"
        android:icon="@drawable/ic_menu"
        android:title="test"
        myapp:showAsAction="always" />
</menu>

In my FragmentMain.java file I have the following 2 mothods:

Code:
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    Log.i(TAG, "msg1");
    setHasOptionsMenu(true);
    Log.i(TAG, "msg2");
    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, "myMsg");
    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, "OnCreateOptionsMenu");
    menuInflater.inflate(R.menu.top_menu, menu);
    super.onCreateOptionsMenu(menu, menuInflater);
}

However, by monitoring the LogCat, this onCreateOptionsMenu method never gets called, and the toolbar with the menu icon is never created in the fragment, just the original one created in the fragment_main_layout.xml file.
 
Back
Top Bottom