Android: ToggleButton Listener

Try toggleAlarm.isChecked() too see if the button is checked or not.


try this, May be the problem is with import

toggleAlarm.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if(isChecked)
    {
        Log.d("alarmCheck","ALARM SET TO TRUE");
        sched.setAlarm(true);
    }
    else
    {
        Log.d("alarmCheck","ALARM SET TO FALSE");
        sched.setAlarm(false);
    }

}
});

In case toggleAlarm.isChecked() does not work for you you could always.

boolean _isChecked = false;
((ToggleButton) d.findViewById(R.id.toggle_alarm)).setOnClickListener(new OnOnClickListener() {
    public void onClick(View arg0) {
        _isChecked = !isChecked;            
        if(_isChecked()) {
            Log.d("alarmCheck","ALARM SET TO TRUE");
            sched.setAlarm(true);
        }
        else {
            Log.d("alarmCheck","ALARM SET TO FALSE");
            sched.setAlarm(false);
        }
    }
});

Tags:

Android