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

Asked

Viewed 599 times

2

Good afternoon, my question is this::

I have a submenu, where the user will select the map Layer.

These Submenu’s will have an image of check in to indicate which one is selected.

Follows 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.

A Case:

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

Thanks for your cooperation! Cordial Greetings,

  • You want to know how to change the checkbox image ?

  • Not exactly, I want to change the menu icon.

2 answers

3

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

Create the global variable:

private Menu menu;

in his onCreateOptionsMenu() 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()
  • If it is a Sub Menu, how do I get the reference of the children? In the code above, it changes the image of action_layers the Menu that contains the values! Thanks in advance!

  • Amended response to address your doubt completely. Thank you.

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;
    }

Browser other questions tagged

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