0

How can I create a Switch button in Android Studio (Java) that changes its track/thumb color when its state is on ON (ex. black in ON state, red in OFF state). I already tried to access to the colors in the tack.xml or color.xml but it don't know how to change them every time I turn the button ON/OFF especially the Background color of the Switch track (I was already able to change the Background of the whole Btn). Do you know how I could change the Background color of the Switch track? Here is important part of my code MainActivity:

private TextView switchState;
private Switch mySwitch;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    switchState = (TextView) findViewById(R.id.switchState);
    mySwitch = (Switch) findViewById(R.id.mySwitch);
    track = (trackColor) findViewById(R.id.trackColor);   //**!!

    // set the Switch to off
    mySwitch.setChecked(false);
    //now you need a listener to check changes in state
    mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
            if (isChecked) {
                switchState.setText("Switch is ON!");

                mySwitch.setBackgroundColor(Color.RED);

                trackColor.setBackgroundColor(Color.RED); //**!!

            } else {
                switchState.setText("Switch is OFF!");

            }
        }
    });

}

(!!) it cannot resolve the symbol track and trackColor and I can't explain why.

track.xml

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle">
    <size android:height="40dp"  />
    <gradient
        android:id="@+id/trackColor"/>
    <gradient
        android:height="40dp"
        android:startColor="#2979FF"
        android:endColor="#FFFF00"/>

</shape>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.nick.Simple_Flashlight.MainActivity"
    android:background="@drawable/radial_background"
    >



    <Switch
        android:id="@+id/mySwitch"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:switchMinWidth="200dp"

        android:rotation="270"
        android:thumb="@drawable/highlight_black_96x96"

        android:track="@drawable/track" />




</RelativeLayout>

0 Answers0