Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

10,734

Solution 1

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:

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.

Solution 2

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.

Solution 3

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.

Solution 4

The switch view operates differently depending on which version of Android you are using. This might cause an issue if your app is being run in older versions or newer versions. To solve this, instead we can use SwitchCompat which operates the same on all versions currently.

To adjust this, go to xml code, instead of switch

// change from "Switch"
<androidx.appcompat.widget.SwitchCompat
        android:id="@+id/main_activity_sw_simulate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="10dp"
        android:layout_marginTop="9dp"
        android:fontFamily="@font/coda"
        android:text="@string/switch_text"
        android:textColor="@color/onyx"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        tools:ignore="TouchTargetSizeCheck" />
Share:
10,734
Denis
Author by

Denis

To the person who is reading this, I wish him a healthy, peaceful and happy life ...

Updated on June 15, 2022

Comments

  • Denis
    Denis about 2 years

    What is the difference between these 2 objects (SwitchCompat and SwitchMaterial)? I have tried them and visually they are identical.

    By the way, why did they remove the Switch class? Do you know which UI element is supposed to replace it in the future?

  • Sollace
    Sollace over 3 years
    Yes, but WHY did they make this change? Why can't we just keep using Switch the same way we always have? What does the SwitchMaterial and SwitchCompat do that makes it worth converting?
  • NeverEndingQueue
    NeverEndingQueue about 2 years
    Is there anyway to provide description, summary to SwitchCompat in a similar way as it is possible to provide one using: SwitchPreferenceCompat, by using the e.g. app:summaryOff attribute?