0

//set the Volume switch to ON

    swVolume.setChecked(true);

//attach a listener to check for the changes in state

    swVolume.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
           @Override
           public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
           }
    });

//check the current state before we display the screen

      if(swVolume.isChecked()){
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            am.setStreamVolume(
                AudioManager.STREAM_RING,
                am.getStreamMaxVolume(AudioManager.STREAM_RING),
                0);       
      }
      else {
            swVolume.setChecked(false);
      } 

Can someone tell me why can't I add the if else code into the listener? and is the listener function just to view the switch status?

I saw one example on the internet:

public class MainActivity extends Activity {


 private TextView switchStatus;
 private Switch mySwitch;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  switchStatus = (TextView) findViewById(R.id.switchStatus);
  mySwitch = (Switch) findViewById(R.id.mySwitch);

  //set the switch to ON 
  mySwitch.setChecked(true);
  //attach a listener to check for changes in state
  mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

   @Override
   public void onCheckedChanged(CompoundButton buttonView,
     boolean isChecked) {

but I don't really know why do I need to type the if-else statement twice, cause even if I remove the if-else statement here the code is still working fine. Can someone explain to me?

    if(isChecked){
     switchStatus.setText("Switch is currently ON");
    }else{
     switchStatus.setText("Switch is currently OFF");
    }

   }
  });

  //check the current state before we display the screen
  if(mySwitch.isChecked()){
   switchStatus.setText("Switch is currently ON");
  }
  else {
   switchStatus.setText("Switch is currently OFF");
  }
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  // Inflate the menu; this adds items to the action bar if it is present.
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }

} 

Thank you so much. :)

BEX
  • 187
  • 4
  • 21
  • What's preventing you from putting the `if-else` in the listener? – Mike M. Jan 21 '17 at 02:23
  • the code would not work if I put the if-else in the listener – BEX Jan 21 '17 at 02:25
  • I guess I'm not clear on what you're trying to do. You want to change the volume with the switch? Or just use the switch as a volume indicator? – Mike M. Jan 21 '17 at 02:35
  • I want to use it as a volume indicator. The switch is disabled. The user can't change the ringer mode by clicking the switch but I want to detect the change when user changes the volume with their phone +/- volume butttons. Meaning when the volume is not max the switch will automatically turn off but when the volume is at max the switch will be turned on. – BEX Jan 21 '17 at 02:41
  • Then, yeah, your last supposition is correct. An `OnCheckedChangeListener` isn't going to help you with that. It only fires when the `Switch`'s state changes. – Mike M. Jan 21 '17 at 02:44
  • but I still need it in my code right? Do you know how to detect the change? – BEX Jan 21 '17 at 02:46
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/133687/discussion-between-bexie-and-mike-m). – BEX Jan 21 '17 at 02:49
  • Well, you used to be able to do it with a `ContentObserver` on the system settings - like is shown in [this post](http://stackoverflow.com/a/14207237) - but I'm not sure if that still works. And, no, you wouldn't necessarily need that listener if you're not allowing the user to change the `Switch`. – Mike M. Jan 21 '17 at 02:55
  • Okay, thank you so much, I will try it out later. :) – BEX Jan 21 '17 at 03:07

0 Answers0