-1

I created a Togglebutton for my push notification service (if the button is checked, notifications will be reveived). I also want, that when I check the button, a checkbox is checkbox. If the button isn't check, the checkbox should disabled.

I used that code, but it do not work. Only when the togglebutton is off, the checkbox is disabled. But it do not work otherwise.

        ToggleButton toggle = (ToggleButton) findViewById(R.id.toogleNotifications);

    toggle.setChecked(Pushbots.sharedInstance().isNotificationEnabled());
    toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Pushbots.sharedInstance().toggleNotifications(isChecked);
            chicken.setEnabled(false);
        }
    });

    if (toggle.isChecked()) {
        chicken.setEnabled(true);
    }
N.Preusche
  • 37
  • 1
  • 2
  • 7

2 Answers2

0

I am not sure what are you trying to implement but to enable/disable the checkbox on toggle change, you should do it like that:

 toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            Pushbots.sharedInstance().toggleNotifications(isChecked);
            chicken.setEnabled(isChecked);
        }
    });

and you don't need this part:

    if (toggle.isChecked()) {
        chicken.setEnabled(true);
    }
Atef Hares
  • 4,715
  • 3
  • 29
  • 61
0
chicken.setEnabled(false);

Should be

chicken.setEnabled(isChecked);

While at it,

if (toggle.isChecked())
 { chicken.setEnabled(true);}

Should be a plain

chicken.setEnabled(toggle.isChecked());
lionscribe
  • 3,413
  • 1
  • 16
  • 21