简体   繁体   中英

Switch support in API 15

I am currently using the Switch Widget which supposedly came in API 14. My project is set to a min API of 15. I am trying to change the background of the Switch widget track OnCheckedChanged

but i get an error that setTrackResource needs a minimum API of 16.. I thought at first i just needed to update my support library but its on v13

I don't want to @supress this warning? is there a way around it?

cameraTypeSwitch = (Switch) findViewById(R.id.cameraTypeSwitch);
        cameraTypeSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            // TODO Auto-generated method stub
                if (buttonView.isChecked()){
                    cameraTypeSwitch.setTrackResource(R.drawable.camera_track_camera);
                }
                else{
                    cameraTypeSwitch.setTrackResource(R.drawable.camera_track_video);
                }
            }
    });

i tried to set the track drawable to have an on checked state like so:

    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@null" android:state_enabled="false"/>
    <item android:drawable="@drawable/camera_track_camera" android:state_checked="true"/>
    <item android:drawable="@drawable/camera_track_video" android:state_checked="false"/>

</selector>

but that doesn't seem to work.. is there anyway to dynamically change the track background in API 15

From Android official documentation

public void setTrackResource (int resId) Added in API level 16

So you need API 16+ to use setTrackResource(int) method (that is, minSDK should be 16 or higher). There's no workaround since this method doesn't exist in API 15 or less, unless you write it yourself.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM