Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

The SwitchMaterial:

  • is provided by the Material Components Library
  • extends the SwitchCompat
  • uses Widget.MaterialComponents.CompoundButton.Switch as default style, using the colors defined in the Theme.MaterialComponents (like colorSecondary, colorSurface and colorOnSurface) and applying the Elevation Overlays in dark mode.

The SwitchCompat:

  • is provided by the androidx appcompat library
  • uses Widget.AppCompat.CompoundButton.Switch as default style

By the way, why did they remove the Switch class?

The Switch class is not removed. It is provided by the android framework like other widgets as Button,TextView.. and the appcompat and material components libraries provide an updated version of them (like AppCompatButton, MaterialButton...).

There is a different with these widgets. Using an AppCompat theme there is the AppCompatViewInflater that automatically replaces all usages of core Android widgets inflated from layout files by the AppCompat extensions of those widgets (for example a Button is replaced by AppCompatButton).
Using the Theme.MaterialComponents there is the MaterialComponentsViewInflater that replaces some framework widgets with Material Components ones at inflation time, provided a Material Components theme is in use (for example a Button is replaced by MaterialButton).

It is NOT true for the SwitchMaterial and the SwitchCompat.The reason for that is due to the AppCompat SwitchCompat not actually extending from the framework Switch class.


https://developer.android.com/reference/androidx/appcompat/widget/SwitchCompat

Switch have a different look for older versions of Android. we use SwitchCompat to have consistent look for all Android versions.

enter image description here

SwitchCompat is a complete backport of the core Switch widget that brings the visuals and the functionality of that widget to older versions of the platform. Unlike other widgets in this package, SwitchCompat is not automatically used in layouts that use the element. Instead, you need to explicitly use <androidx.appcompat.widget.SwitchCompat> and the matching attributes in your layouts.

SwitchMaterial is inherited from SwitchCompat. it is a class that creates a Material Themed Switch.


SwitchCompat

SwitchCompat is an extended version of CompoundButton. SwitchCompat is a version of the old Switch widget which on devices back to API v7. It does not make any attempt to use the platform provided widget on those devices which it is available normally.

SwitchMaterial

It's an extended version of SwitchCompat. It creates a Material Themed Switch. This class uses attributes from the Material Theme to style a Switch.Because SwitchCompat does not extend Switch, you must explicitly declare SwitchMaterial in your layout XML.