I wanted to make settings fragment for my app so I added preference dependency to my build.gradle
def preference_version = "1.1.1"
implementation "androidx.preference:preference:$preference_version"
implementation "androidx.preference:preference-ktx:$preference_version"
but when I want to use any class from this library in XML Layout like for example
<PreferenceScreen
xmlns:app="http://schemas.android.com/apk/res-auto">
<SwitchPreferenceCompat
app:key="notifications"
app:title="Enable message notifications"/>
<Preference
app:key="feedback"
app:title="Send feedback"
app:summary="Report technical issues or suggest new features"/>
</PreferenceScreen>
I'm getting errors:
- Cannot resolve class PreferenceScreen
- Cannot resolve class SwitchPreferenceCompat
- Cannot resolve class Preference
I tried to build project after using /gradlew clean but nothing helped, I tried to use different versions of dependencies, different versions of gradle and use only dependency for java and only dependency for kotlin multiple times with different configurations but nothing helped. What is strange outside of XML files everything seems to work just fine and I can use classes from this library just fine, but I need to make layout with PreferenceScreen class and it's not recognising it.
CodePudding user response:
Did you put the xml file under the "xml" folder?
It must be placed under "xml" folder not under "layout" folder
You have to use a PreferenceActivity and call the file with:
addPreferencesFromResource(R.xml.settings);
Official documentation: https://developer.android.com/guide/topics/ui/settings