androidpreferencepreferencefragment

PreferenceFragmentCompat is not taking the correct androidx PreferenceScreen


I am trying to create a preference screen using androidx.preference:preference:1.0.0, however, when I create the settings xml file following this guide https://developer.android.com/guide/topics/ui/settings.html, it is taking the platform Preferences.

As it is taking android.preference, I can't use 'SeekBarPreferecewhich is only inandroidx`, that's why it shows a warning.

enter image description here

I can change the package name to fit androidx ones. However, now it doesn't find it and can check the reference (it finds PreferenceScreen and PreferenceCategory but no SeekBarPreference which is in the same package.

enter image description here enter image description here

Has it happenened to any of you ? Here is the code:


class SettingsFragment : PreferenceFragmentCompat() {

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        setPreferencesFromResource(R.xml.settings, rootKey)
    }

}

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

    <PreferenceCategory
        app:title="@string/settings_notifications">

        <SwitchPreference
            app:key="notifications"
            app:title="@string/settings_news"
            android:icon="@drawable/menu_icon_news"
            android:summaryOff="@string/settings_news_off"
            android:summaryOn="@string/settings_news_on"/>

        <SeekBarPreference
            app:key="feedback"
            app:title="@string/settings_aviso_level"
            android:max="5"
            android:defaultValue="5"
            android:icon="@drawable/ic_notifications_black_24dp"

            />

    </PreferenceCategory>
</PreferenceScreen>

Edit: I was having this issue in Android Studio 3.4 and it is fixed in 3.5


Solution

  • What version of Android Studio are you using? This has been fixed in 3.5, so if you are using the latest releases it should work.

    In any case, this is just a warning that studio displays - it doesn't mean that it is using android.preference.* classes. If you are using the xml file from within androidx.preference.*, i.e using a PreferenceFragmentCompat, then it will correctly use the androidx classes - so it should work fine, and you can just ignore this until you update your Android Studio.

    You can either just use the class name, like SeekBarPreference, or the fully qualified name, androidx.preference.SeekBarPreference and both should work when building and running the app.