-2

How do I change a button image play and stop programmatically?

public void eventOccured(int id) {
                Log.i("sat", "Clicked on " + id);




                if (id==4){


                    if (sound_Off){
                        sound_Off= false;
                        if (editor != null) {
                             editor.putBoolean("prefSoundOnOff",false);
                             editor.commit(); // Very important to save the preference
                             }

                    } else {
                        sound_Off= true;
                        if (editor != null) {
                             editor.putBoolean("prefSoundOnOff",true);
                             editor.commit(); // Very important to save the preference
                             }

                    }
                }       
            }
        });   
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

1

use findViewById() to get your imageview and cast it to imageview.

Then you can set the Image in the if statements.

 ImageView imgview = (ImageView)findViewById(R.id.myImageViewId);
 imgView.setImageResource(R.id.myicon);

Where myImageViewId is the ID of the imageview, set in your xml layout file.

IAmGroot
  • 13,760
  • 18
  • 84
  • 154
0

Create a selector in the drawables with true and false options with eatch image, then apply the selector to the imageview with setImageResource

edit: example i saw in other thread https://stackoverflow.com/a/14024007/3419242

Community
  • 1
  • 1
xanexpt
  • 713
  • 8
  • 20