Switch button always checked (on) when starting the actvitity (android application)

3.6k Views Asked by At

Im making an application where users are able to enable/disable the vibration of the phone. For this, I put a switch. Here is my code:

    public class settings extends Activity {


        public static boolean vibrationOnOff;

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

            final Switch tButton = (Switch) findViewById(R.id.vibration);

            tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

                @Override
                public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
                    if(isChecked){
                        tButton.setChecked(true);
                        vibrationOnOff = true;
                    }else{

                        tButton.setChecked(false);
                        vibrationOnOff = false;
                    }

                }
            });

        }
}

My question: everytime when I start this activity, the switch button is always isChecked()/always on. When I uncheck the switch and go back to another acvitiy and open settins activity again, it is isChecked again.

How can I solve this?

Here is a similar question, but it did not solve my problem: Android ToggleButton always check

2

There are 2 best solutions below

8
On

You could do the following;

public class MainActivity extends Activity {
    private AudioManager mAudioManager;
    private boolean mPhoneIsSilent;

    @Override public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     mAudioManager = (AudioManager)getSystemService(AUDIO_SERVICE);  
     checkIfPhoneIsSilent();
     setButtonClickListener();
}

    private void setButtonClickListener() {
    Button toggleButton = (Button)findViewById(R.id.toggleButton);    
    toggleButton.setOnClickListener(new View.OnClickListener() { 
    public void onClick(View v) {
        if (mPhoneIsSilent) { 
        // Change back to normal mode mAudioManager   
        .setRingerMode(AudioManager.RINGER_MODE_NORMAL); mPhoneIsSilent = false; 
        } else { 
        // Change to silent mode mAudioManager  
        .setRingerMode(AudioManager.RINGER_MODE_SILENT); mPhoneIsSilent = true; 
}
// Now toggle the UI again 
toggleUi(); 
        } 
    }); 
}

// Checks to see if the phone is currently in silent mode.

private void checkIfPhoneIsSilent() { 
    int ringerMode = mAudioManager.getRingerMode();
    if (ringerMode == AudioManager.RINGER_MODE_SILENT) { 
    mPhoneIsSilent = true; 
       } else { 
    mPhoneIsSilent = false; 
    } 
}

//Toggles the UI images from silent to normal and vice versa.

private void toggleUi() {
    ImageView imageView = (ImageView) findViewById(R.id.phone_icon);
    Drawable newPhoneImage;
    if (mPhoneIsSilent) {
    newPhoneImage = getResources().getDrawable(R.drawable.phone_silent);
    } else {
    newPhoneImage = getResources().getDrawable(R.drawable.phone_on); } 
    imageView.setImageDrawable(newPhoneImage);
}       
    @Override protected void onResume() {
    super.onResume();
    checkIfPhoneIsSilent(); 
    toggleUi(); 
}
1
On

First you have to determine the current state of the phone. So if the phone is in Virbration mode, keep the switch checked else unchecked.

To determine the phone state,

boolean isVibrateOn = false;
AudioManager mAudioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int ringerMode = mAudioManager.getRingerMode();
if(ringerMode == AudioManager.RINGER_MODE_VIBRATE)
    isVibrateOn = true;
else
    isVibrateOn = false;

Then set the state of switch appropriately,

final Switch tButton = (Switch) findViewById(R.id.vibration);
tButton.setChecked(isVibrateOn);

tButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
       @Override
       public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
           if(isChecked){
               tButton.setChecked(true);
               vibrationOnOff = true;
           }else{
               tButton.setChecked(false);
               vibrationOnOff = false;
           }

       }
 });

Hope this solves your problem. ;) :)