Search code examples
androidservicetogglebutton

Problems by Toggle Button


My toggleButton are not working properly, what can i do?? If i press toggleButton1 then ultimately toggelButton2 got pressed,as it is not getting the code of toggelButton1. But toggelButton2 is working perfectly Please any one can help me, in fixing my Problem

ToggleButton toggle1, toggle2;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_act);

     toggle1= (ToggleButton) findViewById(R.id.toggleButton1);
     toggle2= (ToggleButton) findViewById(R.id.toggleButton2);

       SharedPreferences spref = getSharedPreferences(null, 0);
        if(spref.getBoolean("one", false)) {
            toggle1.setChecked(true);
            toggle2.setEnabled(false);    
        } else if(spref.getBoolean("two", false)) {
            toggle2.setChecked(true);
            toggle1.setEnabled(false);    
        }

        toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {

              toggle2.setEnabled(!isChecked);
              getSharedPreferences(null, 0).edit()
              .putBoolean("one", isChecked)
              .apply();
           startService(new           Intent(MainActActivity.this,LockScreenService.class));
            }
        });
             toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
               toggle1.setEnabled(!isChecked);
               getSharedPreferences(null, 0).edit()
                   .putBoolean("two", isChecked)
                   .apply();
startService(new Intent(MainActActivity.this,PinLockService.class)); }}); }} 

Solution

  • SharedPreferences spref = getSharedPreferences(mContext, MODE_PRIVATE);
        if(spref.getBoolean("one", false)) {
            toggle1.setChecked(true);
            toggle2.setChecked(false);    
        } else if(spref.getBoolean("two", false)) {
            toggle2.setChecked(true);
            toggle1.setChecked(false);    
        }
    
        toggle1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
            {
    if(isChecked){
    spref.edit()
              .putBoolean("one", isChecked).commit();
    
           startService(new           Intent(MainActActivity.this,LockScreenService.class));
            }
    }
        });
             toggle2.setOnCheckedChangeListener(newCompoundButton.OnCheckedChangeListener() {
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked){
    spref.edit()
              .putBoolean("two", isChecked).commit();
    
           startService(new           Intent(MainActActivity.this,PinLockService.class));
            }
    }
        }}); }}