33

A Switch is a two-state toggle switch widget that can select between two options and a SwitchCompat is a version of the 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.

Given that both are available to any modern Android 4+ app developer, what are the reasons to use one or another? What are the core differences?

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
  • 2
    I think only difference is the Api support as mentioned in this post http://stackoverflow.com/questions/33604164/difference-between-switch-and-switchcompat-in-android. – Sunil Sunny Dec 20 '16 at 09:49

1 Answers1

84

There is a huge difference. Switch is platform dependent. It can look differently on different version systems. On post-lollipop devices it inherits from Material Design styles, on pre-lollipop it inherits from holo styles.

SwitchCompat inherits from Material Design on every system version.

Of course context Activity must be AppCompat one.

Using components from support libraries you ensure the same behaviour on all system versions.

SwitchCompat and Switch image on Android 4 and Android 7

Simon Warta
  • 10,850
  • 5
  • 40
  • 78
R. Zagórski
  • 20,020
  • 5
  • 65
  • 90
  • Thanks a lot. Let me add a note why that matters: using SwitchCompat I do not have to worry about the "on"/"off" texts on older devices, which make no sense in my "yes"/"no" context. – Simon Warta Dec 20 '16 at 10:00
  • Those texts are in resources in appCompat library. So it is translated into all languages in support libraries, which practically means all that android supports. – R. Zagórski Dec 20 '16 at 10:13
  • But SwitchCompat has no text at all, has it? – Simon Warta Dec 20 '16 at 10:15
  • 1
    It may have (attribute [`textOn`](https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html#attr_android:textOn)), you can still customise `SwitchCompat`. By default it does not have. – R. Zagórski Dec 20 '16 at 10:18
  • Thanks again for the answer. I added an overview image supporting your text. Hope you don't mind. – Simon Warta Dec 21 '16 at 09:36
  • 7
    this whole "Compat" thing is such a joke. What are we going to have after Material style is considered outdated... SwitchCompatNew ? – Someone Somewhere Jul 02 '18 at 12:38