Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: ToggleButton Listener

Tags:

android

I have this code here

ToggleButton toggleAlarm = (ToggleButton) d.findViewById(R.id.toggle_alarm);
toggleAlarm.setOnCheckedChangeListener(new OnCheckedChangeListener() {
    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);
        }
    }
});

I have to keep track if its ON or OFF. But when I logged something to logcat where it is on or off, it won't do a thing. I don't know, what is wrong, because on my other code same, syntax but it works I just copy paste it and change only the ToggleButton variable.

EDIT

I have observed, with the help of cdr. Powell of course, that when you put this code block, the one that I have posted, inside another anonymous listener, say listener for a save button, the checkOnChangedListener is broken, it doesn't function well inside another anonymous listener, but the one thing that I don't understand is that, there is also a outer listener in my code, it is like a button to display a dialog box and inside that dialog box, there is an add button that opens another dialog box which has that toggle button and another button for save or add which closes that dialog and returns to the previous dialog which will then display the newly added record, so anyone of you have an idea why is it broken when i put it inside a listener for a save button but works fine in a outer listener.

like image 451
lemoncodes Avatar asked Aug 02 '12 11:08

lemoncodes


People also ask

How do you know if toggle button is pressed?

To check current state of a toggle button programmatically we use isChecked() method. This method returns a Boolean value either true or false. If a toggle button is checked then it returns true otherwise it returns false.

How do you use toggle switch on Android?

A toggle button allows the user to change a setting between two states. You can add a basic toggle button to your layout with the ToggleButton object. Android 4.0 (API level 14) introduces another kind of toggle button called a switch that provides a slider control, which you can add with a Switch object.

What is toggle view?

Toggle refers to the act of switching from one view, setting, or function to another. It implies that there are only two possible options and that a user is switching between them, and usually these options are on or off for a specific preference.


2 Answers

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);
    }

}
});
like image 191
Mohsin Naeem Avatar answered Sep 19 '22 14:09

Mohsin Naeem


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

like image 30
Todd Davies Avatar answered Sep 18 '22 14:09

Todd Davies