How can I remove this warning - "SwitchPreferenceCompat is not allowed here"?

In android project inside res / xml / filename 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 cannot be resolved here. How to fix this warning. The app works without any problem. But still I would like to know how to solve this problem.

+6


source to share


4 answers


Try replacing PreferenceScreen

with android.support.v7.preference.PreferenceScreen

in the root tag of your preference XML file (in this case it's pref_visualizer.xml).



+27


source


Use SwitchPreference

instead SwitchPreferenceCompat

.



0


source


Use full classpath in xml to avoid problem:

<com.example.CustomSwitchPreference..

      

0


source


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 updating the parent tag to v7). however, the instructions and guidelines on the Android developer site (developer.android.com/guide/topics/ui/settings) should use the AndroidX settings library. so I suggest moving the project to AndroidX and using "androidx.preference.PreferenceScreen" for the parent tag.

0


source







All Articles