Skip to content Skip to sidebar Skip to footer

How To Make Item Visible Only If Statement Boolean In Android

I have static constant top navigation menu with the following item

Solution 1:

You can do as follows:

@OverridepublicbooleanonCreateOptionsMenu(Menu menu) {
     //inflate menu
     getMenuInflater().inflate(R.menu.top_navigation_menu, menu);

    MenuItemaction_camera_menu_item= menu.findItem(R.id.action_camera);
    MenuItemaction_gallery= menu.findItem(R.id.action_gallery);

    action_camera_menu_item.setVisible(conditionOK);
    action_gallery.setVisible(conditionOK);

    returntrue;
 }

Solution 2:

Assuming your "condition" is available while onCreateOptionsMenu being called, you can get your menu item by ID like this menu.findItem(...) and then do setVisible(boolean visible) for it.

Post a Comment for "How To Make Item Visible Only If Statement Boolean In Android"