As I was following an old tutorial (Créez des applications pour Android -> openclassroom) I got stuck on this deprecated method addPreferencesFromResource(int id)
from the PreferenceActivity class.
So my question is :
What is the new way of creating Preferences in Android ?
I liked the solution from this post: http://alvinalexander.com/android/android-tutorial-preferencescreen-preferenceactivity-preferencefragment
.. because it seems the most compact for someone that just needs something very basic up and running quickly. It has only one .java file and two small xml files.
Add the following files to your project. Use the order they are listed in to avoid compile errors.
Add /res/values/array.xml
Add /res/xml/preferences.xml
Add the Activity code
I found this post (What to use instead of “addPreferencesFromResource” in a PreferenceActivity?) that help me understand that you have to go through a
PreferenceFragment
in order to do it.In the following explanation I use
your.package.
just to show that you have to put the package name. Everybody has its own package so please replace it with your package.lets begin :
1. Preference Fragment
Create your PreferenceFragment class
Then the associated xml resource
That's all for the
Fragment
part.2. Preference Activity
Create the PreferenceActivity class
Do not forget to override
isValidFragment(String fragmentName)
method as you will get punched in the face by your application ! ;) More seriously I have no idea why you need to do this but it is needed. If someone has an explanation about this I'd gladly read it :)EDIT :
Thanks to kirtan403 I now know why it is needed : it has to be set because of an (android framework fragment injection).
As you can see in the
onBuildHeaders(List<Header> target)
we load another xml file that contain the headers of the preference. In short, headers are the left part of the preference and the fragment are the right part (for tablet). For a phone you will first have the headers and when you click on an item the corresponding fragment will be put on top of the headers list.Read this article (Multi-pane development in Android with Fragments - Tutorial) the images explain themselves.
Then the associated xml resource
As you may have noticed in the header section you have :
android:fragment="your.package.MyPreferenceFragment"
This will act as a Link to the fragment you want to show. On Tablet it will load on the right part and on the phone it will load on top of the current view.
3. Android Manifest
Now what you should do is to add your Activity to the
AndroidManifest.xml
file.Inside the
application
section add these lines :You will probably tell me :
But DO NOT PUT THIS as you will never load your fragment on phone. This error was solved by a great man ! This is the link to his blog (Android header preferences on small screen/phone).
4. Start the Preferences from Menu
Finally you need to add the ability to show this Preference !! To do so you will need 3 things :
The Menu
Loading this Menu in your Main activity (not the PreferenceActivity) under the method
onCreateOptionsMenu(Menu menu)
Starting the
MyPreferenceActivity
Activity when you click on that button.For that you will need to override the
onOptionsItemSelected(MenuItem item)
method in your Main activity.Et voila les amis !
I haven't tested this code. I took it and modified it from my own code so I may have not well copy pasted things. If you encounter errors tell me, I'll try to figure out the problem and fix this.
I hope this post will help some people out there :D
Cheers !