Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

toggle button event not working on various devices

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.

like image 551
user788511 Avatar asked Feb 19 '23 19:02

user788511


2 Answers

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 */
    }
    ...
}
like image 177
Ronnie Avatar answered Feb 27 '23 20:02

Ronnie


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();
             }
             }
             });
like image 41
Yash Avatar answered Feb 27 '23 22:02

Yash