Crea sito

Android: PreferenceActivity Tutorial

What is PreferenceActivity?

When you try to develop an android app, you need a mechanism to manage

your application's preferences interaction (author, settings, contacts, licenses,....) and

the android platform gives you this possibility throught the PreferenceActivity

(that you can connect with sharedpreference easily).

In this way you can visualize and manage all that settings that your app need.




(*) The image on your right it's the result that I have in my application but that rapresent the foundaments

of what you can do with this tutorial.

What to do?

In this simply example we'll have an overview on the PreferenceActivity.

All you have to do:

  - declare in the manifest.xml the SettingsActivity

  - build an activity that extends PreferenceActivity

Now it's time to start the implementation of some issues that this particular activity offers to us:

We will create/implement some Preference object that corrispond to the actions associated with them.

For example in the My School Manager image, Facebook (contact me) directs you on my facebook page.


The tree below represents what you see in the image:


--- -PreferenceCategory (Information)

-------- Preference (My School Manager - 1.6)

-------- Preference (Author - Simone Casagranda)

-------- Preference (Twitter - Follow my tweets)

-------- Preference (Facebook - Contact me)

--- -PreferenceCategory (App Information)

-------- Preference (Rank - Leave a rank to my app. Thanks!)

-------- Preference (Help(?) - My School Manager's guide)


An example of Preference:


Preference pref = new Preference(context);

pref.setTitle("My School Manager");



Then you have to attach you preference to a category and the category to a preferenceScreen.


You will find some code in the uploaded project. The project is base on code, not xml, because I want

to give you the possibility to manipulate and extend it.