WannaGetHigh WannaGetHigh - 1 year ago 186
Android Question

How do you create Preference Activity and Preference Fragment on Android?

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 ?

Answer Source

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


    public class MyPreferenceFragment extends PreferenceFragment
        public void onCreate(Bundle savedInstanceState)

  • Then the associated xml resource

    fragment_preference.xml (in the folder res/xml of your project)

    <?xml version="1.0" encoding="utf-8"?>
                android:title="check it out"
                android:summary="click this little box"/>

    That's all for the Fragment part.

2. Preference Activity

  • Create the PreferenceActivity class


    public class MyPreferenceActivity extends PreferenceActivity
        public void onBuildHeaders(List<Header> target)
            loadHeadersFromResource(R.xml.headers_preference, target);
        protected boolean isValidFragment(String fragmentName)
            return MyPreferenceFragment.class.getName().equals(fragmentName);

    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

    headers_preference.xml (in the folder res/xml of your project)

    <?xml version="1.0" encoding="utf-8"?>
            android:title="Goto: Preference fragment"
            android:summary="An example of some preferences." />

    As you may have noticed in the header section you have :


    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 :

"Oh darling you forgot to put android:launchMode="singleTask" in your actvity"

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

    menu.xml (in the folder res/menu of your project)

    <?xml version="1.0" encoding="utf-8"?>
            android:title="Preferences" />

  • Loading this Menu in your Main activity (not the PreferenceActivity) under the method onCreateOptionsMenu(Menu menu)

    public boolean onCreateOptionsMenu(Menu menu)
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;

  • 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.

    public boolean onOptionsItemSelected(MenuItem item)
            case R.id.preferences:
                Intent intent = new Intent();
                intent.setClassName(this, "your.package.MyPreferenceActivity");
                return true;
        return super.onOptionsItemSelected(item);

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 !

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download