PreferenceActivity in Android

PreferenceActivity -This is the base class for an activity to show a hierarchy of preferences to the user. It is usually  used to create a standard looking settings screen for any application.

The values are stored in SharedPreferences automatically by the PreferenceActivity.

The list of settings, are defined in xml file in xml folder  which is inside the res folder.

The given example explains its usage.

Firstly ,create a xml file in xml folder which is  inside the res folder.


<?xml vesion="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="" >

<PreferenceCategory android:title="Application Settings" >
android:summary="Select background picture"
android:title="Select background type" />
android:dialogTitle="change password"
android:summary="Change lock password"
android:title="change lock password" />

android:summaryOff="Vibration Deactivated"
android:summaryOn="Vibration Activated"
android:title="Vibration Activation" />
android:summaryOff="Locker Disabled"
android:summaryOn="Locker Enabled"
android:title="Enable "/>

android:summaryOff="Home Key Enabled"
android:summaryOn="Home Key Disabled"
android:title="Disable Home Key" />


The layout for the SettingActivity which uses a ListView and all the setting preferences will be displayed in the list view.  The same is given below.


<RelativeLayout xmlns:android=""

<ListView android:id="@android:id/list"
android:layout_height="fill_parent" />


Now, this class extends PreferenceActivity implements OnPreferenceChangeListener.When ever the preference value is changed the OnPreferenceClickListener is used to do the required action.

public class SettingActivity extends PreferenceActivity implements

Preference.OnPreferenceChangeListener {

static ChartBoost cb;

String TAG = "chartboost";

Preferences preferences;



public void onCreate(Bundle savedInstanceState) {


setContentView(R.layout.setting_screen);// list view

addPreferencesFromResource(R.xml.settings);// adding preferences


//fetching the required prefereence

final Preference prefimg = (Preference) findPreference("picture");

//setting PreferenceClickListener on the preference

prefimg.setOnPreferenceClickListener(new OnPreferenceClickListener() {

// if select background is selected

public boolean onPreferenceClick(Preference preference) {

//preference clicked

Intent i = new Intent(SettingActivity.this, GalleryView.class);                          startActivity(i);

// put your code here

return true;


Similarly , you can add action code whenever there is click on the preference setting.

Hence ,PrefrenceActivity  provides massive advantage over Prefrences as saving of preferences is automatic and hassle free.Here’s a screenshot of the example given above:


PreferenceActivity Example

Related Posts:

Difference between sqlite and shared preferences in android

Using shared preferences in android


3 thoughts on “PreferenceActivity in Android

  1. I enjoy what you guys are up too. Such clever work and reporting!

    Keep up the excellent works guys I’ve added you guys to blogroll.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s