Hide / Show Menu

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 to 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?

    
asked by anonymous 26.07.2016 / 17:18

2 answers

1

I do not 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
    
26.07.2016 / 18:25
1

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

rootView.setVisibility (View.GONE);

    
26.07.2016 / 18:15