Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PreferenceScreen has been deprecated by API 29

Tags:

android

I did create preferences XML then I see the PreferenceScreen has been deprecated by API 29. What is the replacement?

And you can see depreciation message here:

enter image description here

like image 522
A.R.B.N Avatar asked Aug 10 '19 06:08

A.R.B.N


People also ask

What is Preferencescreen in Android?

Use the AndroidX Preference Library for consistent behavior across all devices. For more information on using the AndroidX Preference Library see Settings. Represents a top-level Preference that is the root of a Preference hierarchy. A PreferenceActivity points to an instance of this class to show the preferences.

How do I set custom preferences on Android?

It's still possible to customise the appearance of a Preference item though. In your XML you have to declare the root element as android:id="@android:id/widget_frame , and then declare TextView as android:title and android:summary . You can then declare other elements you want to appear in the layout.

What is PreferenceFragmentCompat?

A PreferenceFragmentCompat is the entry point to using the Preference library. This Fragment displays a hierarchy of Preference objects to the user. It also handles persisting values to the device.


2 Answers

It's deprecated in API level 29 base on Google Document and also by the Google recommendation you should use AndroidX Preference Library instead.

You can check AndroidX Preference guide in this link

If you still have the problem after Migrate to Androidx you can use

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

instead of

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

It's not necessary but you can add this implementation into your Gradle too.

implementation 'androidx.preference:preference:X.Y.Z' 
like image 71
Ali shatergholi Avatar answered Oct 07 '22 14:10

Ali shatergholi


As described in the official doc:

This class was deprecated in API level 29.
Use the AndroidX Preference Library for consistent behavior across all devices

like image 21
Gabriele Mariotti Avatar answered Oct 07 '22 12:10

Gabriele Mariotti