askIT

Android Hide and show toolbar menu items in fragment

hide_menu_items.png show_menu_items.png
Fig: You can show and hide icons from the action toolbar in Java.

To hide a icon then you need to get the menu item from xml. Then use the setVisibile in onCreateOptionsMenu.

private MenuItem menuItemEdit;
private MenuItem menuItemDelete;

menuItemEdit = menu.findItem(R.id.action_edit);
menuItemDelete = menu.findItem(R.id.action_delete);

menuItemEdit.setVisible(false);
menuItemDelete.setVisible(false);

Example

res\menu\my_menu.xml

<?xml version="1.0" encoding="utf-8"?>
<menu
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">


    <!-- Add -->
    <item android:id="@+id/actionAdd"
        android:icon="@drawable/ic_menu_action_add"
        android:title="@string/add"
        app:showAsAction="always"/>

    <!-- Edit -->
    <item android:id="@+id/actionEdit"
        android:icon="@drawable/ic_menu_action_edit"
        android:title="@string/edit"
        app:showAsAction="always"/>


    <!-- Delete -->
    <item android:id="@+id/actionDelete"
        android:icon="@drawable/ic_menu_action_delete"
        android:title="@string/delete"
        app:showAsAction="always"/>


</menu>

java\com.yourdomain.package\yourFragment.class

/*- 01 Class Variables -------------------------------------------------------------- */
// Action buttons on toolbar
private MenuItem menuItemEdit;
private MenuItem menuItemDelete;


/*- 05 on Activity Created ---------------------------------------------------------- */
// Run methods when started
// Set toolbar menu items
@Override
public void onActivityCreated(Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);

    // Create menu
    setHasOptionsMenu(true);


} // onActivityCreated

/*- 08 on Create Options Menu -------------------------------------------------------- */
// Creating action icon on toolbar
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {

    // Inflate menu
    ((MainActivity)getActivity()).getMenuInflater().inflate(R.menu.menu_categories, menu);

    // Assign menu items to variables
    menuItemEdit = menu.findItem(R.id.action_edit);
    menuItemDelete = menu.findItem(R.id.action_delete);

    // Hide as default
    menuItemEdit.setVisible(false);
    menuItemDelete.setVisible(false);
}

/*- 09 on Options Item Selected ------------------------------------------------------ */
// Action icon clicked on
// Menu
@Override
public boolean onOptionsItemSelected(MenuItem menuItem) {

    int id = menuItem.getItemId();
    if (id == R.id.action_add) {
        createNew();
    }
    else if (id == R.id.action_edit) {
        edit();
    }
    else if (id == R.id.action_delete) {
        delete();
    }
    return super.onOptionsItemSelected(menuItem);
}


Add comment

avatar_blank_60.png

Comment:

Name:

Email:

Comments

No comments yet. You can be the first one to comment. Just write your comment in the form and click on the submit button.