title: How do I change an image when clicking a button?
body: <p>so i am trying to change an image when a button below it is clicked. I am trying to toggle the phone from normal to silent mode and have a picture change according to the state of the phone.</p>
  
  <p>I am following a book and dont know what is wrong ( I am not copy pasting cuz that creates problems). Good news though. Although the picture does not change when the phone is toggled to silent mode, When I reopen the app with the silent mode already on, the image changes to what its supposed to be when its on silent. And when I toggle it back to normal mode, it works but does not change its image till I close and reopen the app and the system reads the state of the phone. I have no idea whats wrong but heres my code:</p>
  
  <pre><code>private AudioManager mAudioManager;
      private boolean mPhoneIsSilent;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.linear);
  
     mAudioManager= (AudioManager)getSystemService (AUDIO_SERVICE);
      checkIfPhoneIsSilent();
     setButtonClickListener(); 
     toggleUi();
  }
  
  
  private void setButtonClickListener(){
  
      Button toggleButton=(Button) findViewById(R.id.toggleButton);
      toggleButton.setOnClickListener(
  
              new View.OnClickListener(){
  
                  public void onClick(View v){
  
                      if (mPhoneIsSilent){
                          mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
              mPhoneIsSilent=false;
              }
              else{
                  mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
              mPhoneIsSilent=true;
              }
          }
  
      }
              );
  }
  
  
  
  private void toggleUi() {
      ImageView imageView=(ImageView)findViewById(R.id.phone_icon);
      Drawable newPhoneImage;
      if(mPhoneIsSilent)
          newPhoneImage=getResources().getDrawable(R.drawable.mute);
      else
          newPhoneImage=getResources().getDrawable(R.drawable.unmute);
      imageView.setImageDrawable(newPhoneImage);
      setContentView(R.layout.linear);
  }
  
  
      private void checkIfPhoneIsSilent()
      {
          int ringerMode=mAudioManager.getRingerMode();
          if(ringerMode==AudioManager.RINGER_MODE_SILENT)
              mPhoneIsSilent=true;
          else mPhoneIsSilent=false;
      }
  </code></pre>
  
  <p>and here is the XML to go along with it:</p>
  
  <pre><code>       &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical" &gt;
  
   &lt;ImageView
        android:id="@+id/phone_icon"               
        android:layout_width="wrap_content"               
        android:layout_height="wrap_content"               
        android:layout_gravity="center_horizontal"               
        android:src="@drawable/unmute" /&gt;
   &lt;Button 
       android:id="@+id/toggleButton"         
       android:layout_width="wrap_content"         
       android:layout_height="wrap_content"          
       android:layout_gravity="center_horizontal"         
       android:text="Toggle Silent Mode"/&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>