0

I have a menu with two items (in one fragment is the edit icon and in the other ones is the new icon(that is working fine)), and an overflow menu with three items inside, my issue is I have an empty space in the top of my overflow menu. My activity has a ViewPager with three fragments. I have tried removing android:scaleType, adding orderInCategory and still the issue.

<item
    android:id="@+id/btn_refresh"
    android:icon="@drawable/ic_action_refresh"
    android:title="@string/action_refresh"
    android:scaleType="fitXY"
    app:showAsAction="always"/>
<item
    android:id="@+id/btn_new"
    android:icon="@drawable/ic_action_add"
    android:title="@string/action_new"
    android:orderInCategory="2"
    app:showAsAction="always"/>

<item android:id="@+id/btn_edit"
    android:icon="@drawable/ic_action_edit"
    android:title="@string/action_edit"
    android:orderInCategory="2"
    app:showAsAction="always"/>

<item android:id="@+id/recalculate_values"
      android:title="@string/recalculate_values"
      android:scaleType="fitXY"
      app:showAsAction="never"/>

<item android:id="@+id/about"
      android:title="@string/about"
      android:scaleType="fitXY"
      app:showAsAction="never"/>

<item android:id="@+id/exit"
      android:title="@string/exit"
      android:scaleType="fitXY"
      app:showAsAction="never"/>

I have tried some answer I found in these post Android - Correct use of invalidateOptionsMenu() and Action Bar shows up empty in Android app , so I call invalidateOptionsMenu() after this code:

    Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayShowTitleEnabled(false);
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeAsUpIndicator(R.mipmap.ic_launcher);

On my activity:

   @Override
public boolean onPrepareOptionsMenu(Menu menu) {

    MenuItem mNew = menu.findItem(R.id.btn_new);
    MenuItem mEdit = menu.findItem(R.id.btn_edit);

    int frag = viewPager.getCurrentItem();

    if(frag == 0) {
        mNew.setVisible(false);
        mEdit.setVisible(true);
        return true;
    }else {
        mNew.setVisible(true);
        mEdit.setVisible(false);
    }
    return super.onPrepareOptionsMenu(menu);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
    switch (menuItem.getItemId()) {
    //manage items
}

And in each fragment, I call in the method onCreateView() setHasOptionsMenu(true); I would appreciate any tips, thanks in advance.

EDIT I attach the xml activity if helps

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="?attr/colorPrimary"
        app:theme="@style/AppTheme"
        app:popupTheme="@style/AppTheme.PopupOverlay" />

    <android.support.design.widget.TabLayout
        android:id="@+id/tabs"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:tabMode="fixed"
        app:tabGravity="fill"/>
</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
    android:id="@+id/viewpager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"  />
Community
  • 1
  • 1
FCF
  • 21
  • 3
  • I'm pointing to any solution. But anyway, I found the else part of `onPrepareOptionsMenu` is returning nothing. You should return `true` from here I guess. – Reaz Murshed Apr 01 '16 at 19:26
  • @ReazMurshed thanks for your time, I modified this part of the code but still the empty space, I will edit with more details with the activity xml. – FCF Apr 01 '16 at 20:08

1 Answers1

0

"I have a menu with two items and an overflow menu with three items inside"

your XML shows 6 items total, not 5. Possibly one of your title strings is empty, resulting in the "empty" item in overflow.

I'd suggest removing the items one at a time, that should help find the culprit.

CSmith
  • 13,318
  • 3
  • 39
  • 42
  • I am sorry, the menu where show 2 items swaps the icons it depends on the fragment, (one fragment has the item edit, and the other ones has the new item) I manage that on the method onPrepareOptionsMenu, that is why there are 6 items – FCF Apr 01 '16 at 19:10