Click on a Menu, and change the icon of the others

2

Good afternoon, my question is this:

I have a subMenu, where the user will select the Map Layer.

These SubMenu's will have a check image to tell you which one is selected.

Follow the xml:

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:com.app.map="http://schemas.android.com/apk/res-auto">
    <item android:id="@+id/action_layers"
        android:icon="@mipmap/ic_layers"
        android:title="@string/camadas"
        com.app.map:showAsAction="always">
        <menu>
            <item android:id="@+id/layer_normal"
                android:title="@string/normal"
                android:icon="@android:drawable/checkbox_on_background">

            </item>
            <item android:id="@+id/layer_satelite"
                android:icon="@android:drawable/checkbox_off_background"
                android:title="@string/satelite">

            </item>
            <item android:id="@+id/layer_terreno"
                android:icon="@android:drawable/checkbox_off_background"
                android:title="@string/terreno">

            </item>
        </menu>
    </item>

</menu>

My question is how to remove the icon from the previous selection.

An Example:

By default it comes with the Normal Layer, when clicking on Satellite, how do I get the Normal reference to remove / change your image ???

Thank you for your cooperation! Best Regards,

    
asked by anonymous 17.09.2015 / 19:54

2 answers

0

Try the following:

     Menu mMenu; // referencia do menu

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.menu_map, menu);
            this.mMenu = menu;   
            return true;
        }

//GUARDA A ULTIMA SELECAO
    int lastSelected = R.id.layer_normal;

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {

        if(R.id.action_layers  == item.getItemId())
            return true;


        if(lastSelected == item.getItemId())
            return false;

        // REMOVA A IMAGEM DO ANTERIOR
        final SubMenu itens = mMenu.getItem(0).getSubMenu();
        if(itens != null)
        {
            if(itens.getItem(0).getItemId() == lastSelected)
            {
                itens.getItem(0).setIcon(android.R.drawable.checkbox_off_background);
            }
            else if (itens.getItem(1).getItemId() == lastSelected)
            {
                itens.getItem(1).setIcon(android.R.drawable.checkbox_off_background);
            }
            else if(itens.getItem(2).getItemId() == lastSelected)
            {
                itens.getItem(2).setIcon(android.R.drawable.checkbox_off_background);
            }
        }
//GUARDA A REFERENCIA PARA A PROXIMA TROCA!
        item.setIcon(android.R.drawable.checkbox_on_background);
        lastSelected = item.getItemId();
        return true;
    }
    
17.09.2015 / 20:34
3

You can create a global menu variable and initialize it in onCreateOptionsMenu() and then use it to change the image as follows:

Create the global variable:

private Menu menu;

on your% w /% Do:

this.menu = menu;

And to change the menu icon do:

   this.menu.getItem(0).setIcon(getResources().getDrawable(R.drawable.ic_launcher));

To get the subMenu just do the following:

  this.menu.getItem(0).getSubMenu()
    
17.09.2015 / 20:07