5

The Androidx Preference library offers, amongst others, two classes for implementing a SwitchPreference in Settings screens:

  1. SwitchPreference
  2. SwitchPreferenceCompat

Both implementations in the AOSP source code at the time of writting this question (AOSP Androidx SwitchPreference and AOSP Androidx SwitchPreferenceCompat) looks mostly the same except for some syleables, etc...

So my question is which one do we have to use, considering compatibility with possible future changes of their implementation

BamsBamx
  • 4,139
  • 4
  • 38
  • 63
  • My first guess is to use SwitchPreferenceCompat because of its 'Compat' suffix, but still need someone to confirm to be sure – BamsBamx May 28 '20 at 23:49
  • 7
    `SwitchPreferenceCompat` uses [`SwitchCompat`](https://developer.android.com/reference/androidx/appcompat/widget/SwitchCompat) widget in its view internally while `SwitchPreference` uses regular `Switch`. It's the only difference. You won't even see it unless you run your app on very old APIs that renders `Switch` using non-material style. – Pawel May 29 '20 at 00:12

1 Answers1

1

The Switch widget is the only difference between them.

In API 21 and above, they are the same, because the system will automatically override the classic widgets with the material style.

See also: Switch vs. SwitchCompat