Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Switch-Button Reset does not work properly

The switch button does not go to the OFF-state in the UI-view also I set an onCheckedChangeListener. It stays on also turns grey. This happens on the Simulator with API15, but does not appear on API19 on my real device. Is it the code or the simulator?
The last sr.setChecked(false) only lets the button turn grey but does not set it to OFF.
Minimal example to reproduce behavior: class var:

Switch sr;
Switch srs;

onCreate includes:

    sr = (Switch) findViewById(R.id.switch_ros);
    srs = (Switch) findViewById(R.id.switch_ros_stream);
    sr.setOnCheckedChangeListener(this);
    srs.setOnCheckedChangeListener(this);

onCheckedChanged includes:

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    buttonView.setChecked(false);
}

EDIT: Instead of Nexus_4_API_15 I tried Nexus_5_API_19 and it works fine. Seems to be the Android-Bug. Image of Simulators

like image 225
qrtLs Avatar asked Oct 30 '22 13:10

qrtLs


1 Answers

Place these two at the top of your class

private Switch sr;
private Switch srs;

Then in onCreate()

@Override
protected void onCreate( Bundle savedInstanceCreate )
{
    [...]// other onCreate() stuff
    sr = (Switch) findViewById(R.id.switch_ros);
    srs = (Switch) findViewById(R.id.switch_ros_stream);
    sr.setOnCheckedChangeListener(this);
    srs.setOnCheckedChangeListener(this);
}

Then OnCheckedChangeListener()

@Override
public void onCheckedChanged( CompoundButton buttonView, boolean isChecked )
{
    switch(buttonView.getId())
    {
        case R.id.switch_ros:
            //boolean wifi_state = isConnected(isChecked);
            sr.setChecked(isChecked);
            break;
        case R.id.switch_ros_stream:
            [...]// other switch function
            srs.setChecked(isChecked);
            break;
    }
}

Edit 1 The onCheckedChangeListener cannot find the switch button you're trying to switch on/off. This is because you're throwing more than one switch button in to the method. You will need to use a switch case or if else statement for the method to distinguish which button you wish to switch.

like image 138
Matthew Carpenter Avatar answered Nov 03 '22 00:11

Matthew Carpenter