0

I have got a static bottom navigation menu. And I need to hide some of menu items. I did investigation but cannot get it working.

Please help.

navigation.xml

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/menuMain" >

    <item
        android:id="@+id/navigation_menu"
        android:icon="@drawable/ic_menu_black_24dp"
        android:title="@string/title_menu" />

    <item
        android:id="@+id/navigation_home"
        android:icon="@drawable/ic_explore_black_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_dashboard"
        android:icon="@drawable/ic_map_black_24dp"
        android:title="@string/title_dashboard" />

    <item
        android:id="@+id/navigation_notifications"
        android:icon="@drawable/ic_settings_black_24dp"
        android:title="@string/title_notifications" />

</menu>

MainActivity

public class MainActivity extends AppCompatActivity {

    MenuItem navigationHome;

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.navigation, menu);
        // Get dynamic menu item
        navigationHome = menu.findItem(R.id.navigation_home);
        return true;
    }

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

        // It does not work
        navigationHome.setVisible(false);
        menu.removeItem(R.id.navigation_home);

        return true;
    }
   ...
}

I mean I have got exactly this approach implemented https://segunfamisa.com/posts/bottom-navigation-view-android and event cannot hide item using item attribute android:visible="false" . Thats weired...

NoWar
  • 36,338
  • 80
  • 323
  • 498
  • You need to call `invalidateOptionsMenu()` to trigger `onPrepareOptionsMenu()`. – Henry Jan 08 '18 at 01:23
  • @Henry Hi! Please share your answer. I did use this method withon `onResume` but it does not work. – NoWar Jan 08 '18 at 01:26

4 Answers4

0

If you are calling invalidateOptionsMenu() in onResume() then it will trigger onPrepareOptionsMenu(). So that's not your problem.

Your problem is in the line navigationHome.setVisible(false);. Instead try this: menu.findItem(R.id.navigation_home).setVisible(false);

Also, you don't need menu.removeItem(R.id.navigation_home); to hide. You can get rid of this.

You onPrepareOptionsMenu() should be like this:

@Override
public boolean onPrepareOptionsMenu(Menu menu) {
    menu.findItem(R.id.navigation_home).setVisible(false);
    return true;
}
Henry
  • 17,490
  • 7
  • 63
  • 98
  • Sorry, bro. It does not. It is still fully visible.It pass thru breakpoint without any error. – NoWar Jan 08 '18 at 01:43
0

Put the code in where you want to hide/show the menu item.

menu.findItem(R.id.navigation_home).setVisible(false/*true*/);
invalidateOptionsMenu();
zhh
  • 2,346
  • 1
  • 11
  • 22
0
 @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.

        getMenuInflater().inflate(R.menu.menu, menu);
        return true;`enter code here`
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        Log.d("sadsad","ds");
        switch (item.getItemId()) {
            case R.id.navigation_home:
                item.setVisible(false);
                Toast.makeText(getApplicationContext(),
                        "Setting...",
                        Toast.LENGTH_SHORT).show();
                break;
        }
        return false;
    }
tinto mathew
  • 216
  • 4
  • 22
  • Hey... Your code doesnot help to hide static 'android:id="@+id/navigation_home"' And even if I do like ` ` it is still visible. wtf... why it is so difficult to hide item in menu??? It should be easy like 2 x 2 – NoWar Jan 08 '18 at 04:38
0

I found solution here

How to dynamically hide a menu item in BottomNavigationView?

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

      BottomNavigationView navigation = findViewById(R.id.navigation);
     navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

// This is a way to hide static menu item
      navigation.getMenu().removeItem(R.id.navigation_home);
      ...
    }
NoWar
  • 36,338
  • 80
  • 323
  • 498