Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p><strong>there are two mistakes in your code</strong></p> <p><strong>1.</strong> this <code>setButtonClickListener();</code> called once in your activity i.e. in <code>onCreate()</code> ! so you r button <code>OnClickListener()</code> will work once !</p> <p><strong>2.</strong> inside <code>toggleUi()</code> don't use <code>setContentView(R.layout.linear);</code></p> <p><strong>Solution for 1.</strong> </p> <p><strong>use following code in your</strong> <code>onCreate()</code></p> <pre><code>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; toggleUi(); } else { mAudioManager .setRingerMode(AudioManager.RINGER_MODE_SILENT); mPhoneIsSilent = true; toggleUi(); } } }); </code></pre> <p>and remove these two lines code from <code>onCreat()</code> </p> <pre><code>setButtonClickListener(); toggleUi(); </code></pre> <p>also remove your <code>private void setButtonClickListener()</code> completely !</p> <hr> <p><strong>Solution for 2.</strong></p> <p>your <code>toggleUi()</code> <strong>should be:</strong></p> <pre><code>private void toggleUi() { ImageView imageView = (ImageView) findViewById(R.id.phone_icon); Drawable newPhoneImage; if (mPhoneIsSilent) newPhoneImage = getResources().getDrawable(R.drawable.edit_btn); else newPhoneImage = getResources().getDrawable(R.drawable.ic_launcher); imageView.setImageDrawable(newPhoneImage); } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload