0

I have a Switch in my activity, User click on it to Enable or Disable settings. I store Switch current condition in a static variable.

Right now, the switch is always in the ON state, even if the user disable it, It disabled on the Activity, but when i re launch the activity its on open state.

I want to know how to set OFF state of switch from code.

<Switch
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/switchNotifications"
    android:layout_centerHorizontal="true"
    android:paddingTop="150dp"
    android:text="@string/notification_settings"
    android:textColor="@color/white"
    android:checked="true"
    android:textSize="18sp"
    android:layout_marginLeft="24dp"
    android:layout_marginRight="24dp"
    android:fontFamily="sans-serif-medium"
    android:theme="@style/SCBSwitch"
    />

JAVA

    switchBtn = (Switch) findViewById(R.id.switchNotifications);
    switchBtn.  //What to write here to OFF switch from CODE.
dev90
  • 7,187
  • 15
  • 80
  • 153

1 Answers1

1

You need to call switchBtn.setChecked(false);

https://developer.android.com/reference/android/widget/Switch.html#setChecked(boolean)

Martin Kukan
  • 151
  • 5