I am testing my application on various models and I have realized that the toggle button ON and OFF event is not working. This is the list of devices:
Samsung YS5360
Samsung Galaxy Note
Samsung S Plus
HTC Sensation XE
HTC Wildfire S
Motorola RAZR
LG Optimus Black
Sony Ericsson Xperia neo V
I am not sure what I am doing wrong as I have followed all Android specifications. These events work on other devices. May I have some help please,anyone?
[RE-EDIT]
This is how I am implementing the listener:
@Override
public void onCheckedChanged(CompoundButton arg0, boolean change) {
if(change){
if(text.containsKey("on")){
// do something
}
}else{
if(text.containsKey("off")){
// do something
}
}
if(text.containsKey("clicked")){
// do something
}
// }
}
};
I check for both ON and OFF states but this seems not to be working on other devices.
Using text to know the state of the ToggleButton
is a bad idea. Use the isChecked
parameter instead. See its documentation.
@Override
public void onCheckedChanged (CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
/* checked. ON */
} else {
/* unchecked. OFF */
}
...
}
Try This Code
toggle=(ToggleButton)findViewById(R.id.tglbtn);
toggle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if(toggle.isChecked())
{
Toast.makeText(getApplicationContext(), "The state is changed to on", Toast.LENGTH_LONG).show();
}
else
{
Toast.makeText(getApplicationContext(), "The state is changed to off", Toast.LENGTH_LONG).show();
}
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With