Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Warning : Use SwitchCompat from AppCompat or SwitchMaterial from Material library

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?

like image 462
Denis Avatar asked Jul 31 '20 12:07

Denis


4 Answers

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.

like image 166
Gabriele Mariotti Avatar answered Sep 28 '22 15:09

Gabriele Mariotti


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.

like image 20
Angel Koh Avatar answered Sep 28 '22 16:09

Angel Koh


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.

like image 2
Shalu T D Avatar answered Sep 28 '22 17:09

Shalu T D


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" />
like image 2
Yilmaz Avatar answered Sep 28 '22 15:09

Yilmaz