简体   繁体   中英

Hide or remove static menu item in Android

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...

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;
}

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

menu.findItem(R.id.navigation_home).setVisible(false/*true*/);
invalidateOptionsMenu();
 @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;
    }

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);
      ...
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM