how can it be done so that when you click on a menu icon it changes to another icon?

0

THIS IS THE XML

<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
     tools:context="org.sfaci.agenda.MainActivity">


 <item android:id="@+id/action_modificar"
     android:title="@string/action_settings"
     android:orderInCategory="100"
     android:showAsAction="ifRoom|never"
     android:icon="@drawable/ic_edit" />

 <item android:id="@+id/action_GuardarDato"
     android:title="@string/action_settings"
     android:orderInCategory="100"
     android:showAsAction="ifRoom|never"
     android:icon="@drawable/ic_check" /> </menu>

THIS THE JAVA

 @Override
     public boolean onOptionsItemSelected(MenuItem item) {
         // Handle action bar item clicks here. The action bar will
         // automatically handle clicks on the Home/Up button, so long
         // as you specify a parent activity in AndroidManifest.xml.
         int id = item.getItemId();

         //noinspection SimplifiableIfStatement
         if (id == R.id.action_settings) {
             return true;
         }
         if (id == R.id.action_modificar){
            if (tvNombreApellidos.isEnabled()&& ivFoto.isEnabled()&&
             tvEmail.isEnabled()&& tvTelefonoFijo.isEnabled()&& tvTelefonoMovil.isEnabled()){
                 tvNombreApellidos.setEnabled(false);
                 ivFoto.setEnabled(false);
                 tvEmail.setEnabled(false);
                 tvTelefonoFijo.setEnabled(false);
                 tvTelefonoMovil.setEnabled(false);
             }else{
                 tvNombreApellidos.setEnabled(true);
                 ivFoto.setEnabled(true);
                 tvEmail.setEnabled(true);
                tvTelefonoFijo.setEnabled(true);
                 tvTelefonoMovil.setEnabled(true);
             }
         }

         return super.onOptionsItemSelected(item);
     }
    
asked by alvin 10.11.2017 в 02:56
source

2 answers

3

You can create a global menu variable and initialize it in onCreateOptionsMenu () and then use it in your onClick (). or in some item of your menu.

private Menu menu;

On your onCreateOptionsMenu ()

this.menu = menu;

In the onClick () method of your button

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

Note: The icons are stored in the mimap folder and the images in   the drawable folder. Why? A san google it occurred to him to separate   these components, in order I suppose, and finally make sure   that your icon or image contains the 5 densities so you can not see   "blurred or distorted your icon", as in the following image:

As a last word, Google has a page where you can download desing material icons ( go to the page ) or you can create your own icon from an image you want in this page .

    
answered by 10.11.2017 / 04:08
source
1

you can do it with a variable

boolean cambioImagen= false

by clicking on the button you can do the following:

if(cambioImagen==false){
  /*Código que cambie la imagen*/
  ...
 //Cambias el valor de la variable
 cambioImagen=true;
}else {
/*Código que cambie la imagen al estado original*/
//Vuelves a poner la variable a su estado original
cambioImagen=false;
}

Basically that would be the idea, I hope and it works for you.

Note: You could say it's the same function as a toggle button.

    
answered by 10.11.2017 в 04:16