Introducing the Preference Activity

The PreferenceActivity class is used to host the preference hierarchy defined using the preferences XML file. To create a new Preference Activity, extend the PreferenceActivity class as follows:

public class MyPreferenceActivity extends PreferenceActivity { }

To inflate the preferences, override the onCreate handler and call addPreferencesFromResource, as shown in the following snippet:

@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences);

Like all Activities, the Preference Activity must be included in the application manifest:

<activity android:name=".MyPreferenceActivity"

android:label="My Preferences"> </activity>

This is all that's required for a simple Preference Activity implementation. To display the application settings hosted in this Activity, open it by calling startActivity or startActivityForResult:

Intent i = new Intent(this, MyPreferenceActivity.class); startActivityForResult(i, SHOW_PREFERENCES);

Mobile Apps Made Easy

Mobile Apps Made Easy

Quick start guide to skyrocket your offline and online business success with mobile apps. If you know anything about mobile devices, you’ve probably heard that famous phrase coined by one of the mobile device’s most prolific creators proclaiming that there’s an app for pretty much everything.

Get My Free Training Guide


Post a comment