3

The overflow menu icon is not showing in my Activity with a tab style navigation layout. The tabs are implemented using a ViewPager, Fragments and a TabLayout.

The overflow menu is showing in my Activities which do not have tabs which makes me think the overflow menu not showing is something to do with my implementation of the tabs navigation.

Here is a basic model of my Activity:

public class MainActivity extends AppCompatActivity {


TabLayout tabLayout;
ViewPager viewPager;

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

    initTabLayout();
}

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

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
         return super.onPrepareOptionsMenu(menu);
}

public void initTabLayout(){

    tabLayout = (TabLayout) findViewById(R.id.tabLayout);
    tabLayout.setTabTextColors(Color.parseColor("#e3e8e7"), Color.parseColor("#7e7e7e"));
    viewPager = (ViewPager) findViewById(R.id.viewPager);
    viewPager.setOffscreenPageLimit(4); //this stops the tabs from losing their data when you change tabs
    //sets up adapter that contains the scrolling pageAdapter and the fragment which is displayed
    viewPager.setAdapter(new ActivityTabAdapter(getSupportFragmentManager(),
            MainActivity.this));
    tabLayout.setupWithViewPager(viewPager);

    viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
        }

        @Override
        public void onPageSelected(int position) {
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
        });
            }
    }

And here is my FragmentPagerAdapter implementation:

public class ActivityTabAdapter extends FragmentPagerAdapter {
    @Override
    public Parcelable saveState() {

        return super.saveState();
    }
final int PAGE_COUNT = 4;
private String tabTitles[] = new String[]{"tab1", "tab2", "tab3", "tab4"};
private Context context;

public ActivityTabAdapter(FragmentManager fm, Context context) {
    super(fm);
    this.context = context;
}

@Override
public int getCount() {
    return PAGE_COUNT;
}

@Override
public Fragment getItem(int position) {
    return PageFragment.newInstance(position + 1);
}

@Override
public CharSequence getPageTitle(int position) {
    // Generate title based on item position
    return tabTitles[position];
}


//These are fragments which hold the layouts of the tabs
public static class PageFragment extends Fragment {
    public static final String ARG_PAGE = "ARG_PAGE";


    Context mContext;

    View tab1rootView;
    View tab2Rootview;
    View tab3RootView;
    View tab4RootView;

    Tab1 tab1;
    Tab2 tab2;
    Tab3  tab3;
    Tab4 tab4;

    private int mPage;

    public static PageFragment newInstance(int page) {
        Bundle args = new Bundle();
        args.putInt(ARG_PAGE, page);
        PageFragment fragment = new PageFragment();
        fragment.setArguments(args);
        return fragment;
    }

    MenuInflater mInflater;
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
                 super.onCreateOptionsMenu(menu, inflater);
        mInflater = inflater;
        inflater.inflate(R.menu.menu_layout, menu);
    }

    @Override
    public void onPrepareOptionsMenu(Menu menu) {
        super.onPrepareOptionsMenu(menu);
        mInflater.inflate(R.menu.menu_layout, menu);
    }

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

        setHasOptionsMenu(true);

        mContext = getActivity();


        mPage = getArguments().getInt(ARG_PAGE);

        Log.v("whichFrag", "" + getArguments().getInt(ARG_PAGE));
    }

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

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {

        setHasOptionsMenu(true);
        tab1rootView = inflater.inflate(R.layout.tab1_layout, container, false);
        tab2Rootview = inflater.inflate(R.layout.tab2_layout, container, false);
        tab3RootView = inflater.inflate(R.layout.tab3_layout, container, false);
        tab4RootView = inflater.inflate(R.layout.tab4_layout, container, false);
        int i = getArguments().getInt(ARG_PAGE);


        if (getArguments().getInt(ARG_PAGE) == 1) {  //if 1, then we inflate tab1rootView layout
            tab1 = new Tab1(tab1rootView, getActivity());
            tab1.initCardViews1();
            return tab1rootView;
        } else if (getArguments().getInt(ARG_PAGE) == 2) { //if 2, then we inflate tab2Rootview layout
            tab2 = new Tab2(tab2Rootview, mContext);
            return tab2Rootview;
        } else if (getArguments().getInt(ARG_PAGE) == 3) {  //if 3, then we inflate tab3RootView layout
            tab3 = new Tab3(tab3RootView, mContext);
                return tab3RootView;
            } else { //if 4, then we inflate tab4RootView layout
                tab4 = new Tab4(tab4RootView, mContext);
                return tab4RootView;
        }
        }

        }
       }

Here is my xml menu:

<menu 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"
tools:context="com.example.heavymagikhq.profilepageofficial.profileEditorJava.Profile_info_editor">

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

<item
    android:id="@+id/action_settings2"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    app:showAsAction="never"
    android:visible="true" />

<item
    android:id="@+id/action_settings1"
    android:orderInCategory="100"
    android:title="@string/action_settings"
    android:icon="@drawable/ic_action_menu"
    app:showAsAction="never"
    android:visible="true" />
</menu>

I have looked at other answers on stackoverflow but I couldn't find the answer.

I'll post more information if you need it.

Thanks in advance.

Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
  • check this link : http://stackoverflow.com/questions/19750635/icon-in-menu-not-showing-in-android – ak sacha Mar 30 '16 at 11:02
  • Thanks, I'll check it out – Micah Simmons Mar 30 '16 at 11:03
  • set app:showAsAction="always" – ak sacha Mar 30 '16 at 11:07
  • I checked out the link. It hasn't helped me thus far as the flow overflow menu still won't show. I did change the menu items to: app:showAsAction="always" and nothing changed. When I switched on show layout bounds I can see the menu items are not getting drawn. Also, when I've used app:showAsAction="never" in another activity the overflow menu does appear – Micah Simmons Mar 30 '16 at 11:12

2 Answers2

3

Okay, so I figured it out and the answer is simple. I didn't need to use setHasOptionsMenu(true); in any of the Fragments' lifecycle stages, and you must in my experience have app:showAsAction="never"as an xml attribute on your at least one of your xml menu items.

If you're extending AppCompatActivity in the Activity which implements the TabLayout and you're also using a Toolbar in that Activity then get a reference to your Toolbar and call this.setSupportActionBar(toolbar); while passing in your Toolbar as a parameter.

It should look like so:

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

And call this in the onCreate() method of your Activity.

Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
0

Try moving setHasOptionsMenu to Oncreate() method of the fragment and use it only once.Also don't inflate the menu twice.Inflate once in OncreateOptionsMenu() and change the visibility of the items in OnPrepareOptionsMenu() instead of inflating again.

Ravi Theja
  • 3,371
  • 1
  • 22
  • 34
  • I will try this but I did solve the issue in a different way – Micah Simmons Mar 30 '16 at 11:47
  • This didn't seem to solve the problem for me, but thanks for your input. Also, I inflated the menu in the onPrepareOptionsMenu() method because I was trying different solutions, but it ultimately wasn't necessary to override the onPrepareOptionsMenu() at all to get the overflow menu showing in my case – Micah Simmons Mar 30 '16 at 11:54