Hide / Show Menu

Asked

Viewed 475 times

0

I have the following menu:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_delete"
        android:orderInCategory="100"
        android:icon="@drawable/ic_delete_white_32dp"
        android:title="@string/action_faults_delete"
        app:showAsAction="always" />
</menu>

I add it in a Fragment as follows:

  @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_delete, menu);
    }

On this screen I have a list of selectable items!

I would like to display the menu only when I have a selected item!

My question is how to hide/show this menu?

2 answers

1

rootView = Inflater.inflate(R.menu.menu_delete, menu);

rootView.setVisibility(View.GONE);

1


I don’t know if it’s the best way, but it works:

private Menu menu;

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_delete"
        android:orderInCategory="100"
        android:icon="@drawable/ic_delete_white_32dp"
        android:title="@string/action_faults_delete"
        android:visible="false" //Adicionado
        app:showAsAction="always" />
</menu>


    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        inflater.inflate(R.menu.menu_delete, menu);
        this.menu = menu;  //Adicionado
    }

In the method you select an item you add the following code.

menu.getItem(0).setVisible(true); // Controla a exibição do item do menu True or False

Browser other questions tagged

You are not signed in. Login or sign up in order to post.