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.

settings.xml

 <?xml version=“1.0” encoding=“utf-8”?>

<PreferenceScreen xmlns:android=“http://schemas.android.com/apk/res/android” >

     <PreferenceCategory android:title=“Application Settings” >

        <Preference

            android:dialogTitle=“Picture”

            android:key=“picture”

            android:summary=“Select background picture”

            android:title=“Select background type” />

        <Preference

            android:dialogTitle=“change password”

            android:key=“password”

            android:summary=“Change lock password”

            android:title=“change lock password” />

 

        <CheckBoxPreference

            android:defaultValue=“true”

            android:key=“cb_vibrate”

            android:summaryOff=“Vibration Deactivated”

            android:summaryOn=“Vibration Activated”

            android:title=“Vibration Activation” />

        <CheckBoxPreference

            android:defaultValue=“false”

            android:key=“cb_enable”

            android:summaryOff=“Locker Disabled”

            android:summaryOn=“Locker Enabled”

            android:title=“Enable “ />

     

         <CheckBoxPreference

            android:defaultValue=“false”

            android:key=“cb_home”

            android:summaryOff=“Home Key Enabled”

            android:summaryOn=“Home Key Disabled”

            android:title=“Disable Home Key” />

     </PreferenceCategory>

</PreferenceScreen>

  

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.    

setting_screen.xml

 <RelativeLayout xmlns:android=“http://schemas.android.com/apk/res/android”

    xmlns:tools=“http://schemas.android.com/tools”

    android:layout_width=“match_parent”

    android:layout_height=“match_parent”

    android:orientation=“vertical” >

 

     <ListView android:id=“@android:id/list”

              android:layout_width=“fill_parent”

              android:layout_height=“fill_parent” />

 </RelativeLayout>

  

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;

       @SuppressWarnings(“deprecation”)

       @Override

       public void onCreate(Bundle savedInstanceState) {

              super.onCreate(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.

Screenshot:

 

screen shot