9

In an Android project inside res/xml/ file name pref_visualizer.xml

<?xml version="1.0" encoding="utf-8"?>

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">

    <SwitchPreferenceCompat
        android:defaultValue="@bool/pref_show_bass_default"
        android:key="@string/pref_show_bass_key"
        android:summaryOff="@string/pref_show_false"
        android:summaryOn="@string/pref_show_true"
        android:title="@string/pref_show_bass_label" />
    <SwitchPreferenceCompat
        android:key="@string/pref_show_mid_key"
        android:title="@string/pref_show_mid_label"
        android:summaryOff="@string/pref_show_false"
        android:summaryOn="@string/pref_show_true"
        android:defaultValue="@bool/pref_show_bass_default" />
    <SwitchPreferenceCompat
        android:key="@string/pref_show_treble_key"
        android:title="@string/pref_show_treble_label"
        android:summaryOff="@string/pref_show_false"
        android:summaryOn="@string/pref_show_true"
        android:defaultValue="@bool/pref_show_bass_default" />

</PreferenceScreen>

The SwitchPreferenceCompat text is highlighted and says it cant be allowed here.How to fix this warning. The app runs without any problem. But still I would like to know How to fix this issue.

Zoe
  • 27,060
  • 21
  • 118
  • 148
Kundan
  • 590
  • 9
  • 21
  • Maybe a [similar](https://stackoverflow.com/q/38131254/7461132) problem. – Abhi Jun 30 '17 at 09:33
  • I tried using that method but it didnt help. I checked and I found that my directories are quite correct. – Kundan Jun 30 '17 at 09:49

6 Answers6

39

Try to replace PreferenceScreen with android.support.v7.preference.PreferenceScreen in the root tag of your preference XML file (the pref_visualizer.xml in this case).

f0rrest
  • 506
  • 4
  • 6
  • Do we need to change the inheriting class for the preference activity as well i.e something like MyPreferenceActivity extends android.support.v7.PreferenceActivity ? – Wajdan Ali Aug 03 '18 at 07:19
4

The "SwitchPreferenceCompat" tag belongs to the com.android.support:preference-v7:28.0.0 library. so you need to match the libraries of the parent "PreferenceScreen" tag and the "SwitchPreference" tag (@f0rrest suggested to update the parent tag to v7). however the instructions and recommendation in the android developer site (developer.android.com/guide/topics/ui/settings) is to use the AndroidX Preference Library. therefore i suggest to migrate the project to AndroidX and use "androidx.preference.PreferenceScreen" for the parent tag.

relbns
  • 106
  • 1
  • 4
2

Your PreferenceScreen's layout cannot be in res/layout/ they have to be in res/xml/

JackWu
  • 1,036
  • 1
  • 12
  • 22
1

As a supplement to f0rrest's answer for the newer versions, you could also replace PreferenceScreen with androidx.preference.PreferenceScreen which will also work.

Michiel
  • 75
  • 6
  • 1
    This worked for me, I wonder why this works? Do you know what library `PreferenceScreen` uses by default when specifying `androidx.preference:preference:1.1.x` in your gradle build settings? Or perhaps this is a different dependency that I need to upgrade instead. – Matt Strom Aug 14 '20 at 05:40
0

Use SwitchPreference instead of SwitchPreferenceCompat.

  • I tried it now the app has started to crash with the error :: Error inflating class (not found)SwitchPreference – Kundan Jul 01 '17 at 09:24
  • 1
    Make sure you have added `compile 'com.android.support:support-v4:21.0.3'` in gradle dependencies. Change the version according to your Build Tools Version –  Jul 02 '17 at 08:22
0

Use the full path to the class in the xml to avoid the issue:

<com.example.CustomSwitchPreference..
Dmitry
  • 149
  • 1
  • 7