Presentation is loading. Please wait.

Presentation is loading. Please wait.

Preference Activity class

Similar presentations


Presentation on theme: "Preference Activity class"— Presentation transcript:

1 Preference Activity class

2 Understanding the “preferenceActivity” class
The responsibility of the PreferenceActivity class is to show a hierarchy of Preference objects as lists, possibly spanning multiplescreens, as shown in the figure

3 When preferences are edited, they are stored using an instance of Shared- Preferences. The SharedPreferences class is an interface for accessing and modifying preference data returned by getSharedPreferences()from any Context object. A PreferenceActivity is a base class that is very similar to the Activity base class. However, the PreferenceActivity behaves a bit differently. One of the most important features that the PreferenceActivity handles is the displaying of preferences in the visual style that resembles the system preferences. This gives your application a consistent feel across the board in regard to Android user interface components. You should use the Preference-Activity when dealing with preference screens in your Android applications.

4 Working with preferennceactivity class
The preferenceActivity shows a hiarchy of preferences on the screen according to preferences file defined in XML The preferences can span multiple screens These preferences automatically save to SharedPrefernces. As an added bonus,the prefernces shown automatically follow the visual style of the system preferences, which allows your application to have a consistent user experience in conjunction with the default Android platform. To inflate and display the PreferenceScreen you just built , add an activity that derives from PreferenceActivity to your application. The Code for this file shown in the next slide

5 The TaskPreference class file is defined by inheriting from PreferenceActivity base class.
The call to addPreferenceFromResource() method is provided with resource ID of the task_preferences.xml file stored in the res/xml directory. Retrieving the EditTextPreference for the default task reminder time by calling the findPreference() method and providing it with the key that was defined in the task_preferences.xml file. On this line I am obtaining the EditText object, which a child of the EditTextPreference, using the getEditText() method. From this object, I set the key listener, wich is responsible for listening to key-press events. I set the key listener through the setKeyListner() method, and by providing it with an instance of DigitsKeyListener, the EditTextPreference only allows digits to be typed into the EditTextPreference for the default reminder time. This is to ensure that the only values passed into the prefences are digits

6 At this point the activity is ready to be used ,This PreferenceActivity allows users to edit and save their preferences. As you can see,this implementation required very little code One more important thing the Preference Activity must be included in the application manifest <activity android:name=".TaskPreferences" android:label="My Preferences"> </activity>

7 Sharing Preferences

8 What Is A Preference? Shared preferences are simply sets of data values that are stored persistently. In other words, the application (or device, for that matter) can be started and stopped without losing the data. The next time the user launches the application, that data will still be available. An individual preference is simply a key-value pair with a specific data type for the value. The preference key is simply a string that uniquely identifies the preference and the value is just that: the value of that preference.

9 For example, your application might want to store the user’s name
For example, your application might want to store the user’s name. The application could have a single preference to store this information: The data type of the preference could be a String The key could be a string called “UserName” The value would be the actual username string, such as “AndroidPowerUser123” or “Bob” A preference can be any of a number of different data types. The following data types are supported by the SharedPreferences class: Boolean values Float values Integer values Long values String values

10 How Shared Preferences Work
The Android SDK includes helpful classes for getting application preferences up and running easily. Preference functionality can be found in the SharedPreferences interface of the android.content package. An application can have multiple sets of application preferences, where each set has a name. For example, a game application might have a set of preferences for user information (user name, , high score, etc.) and a set of preferences for game state (current level, current score, etc.). Preferences can be stored at the activity or application level.

11 Application-level preferences are available across all activities
Application-level preferences are available across all activities. These preferences are retrieved using the application Context class method called getSharedPreferences() by name. For example:

12 There is no limit to the number of sets of shared preferences your application can have. How your application organizes its preferences is up to you. However, you may want to declare your preference set names so that you can easily load and access the preferences from any Activity within your application. For example:

13 An activity can also have private preferences
An activity can also have private preferences. These preferences are only available within the specific Activity class and are not shared with other activities. An activity can only have one set of private preferences. The following code retrieves the activity’s private preferences:

14 Setting Preferences Saving preferences to your application is fairly straightforward. First, you must decide if you want application or activity preferences. Use the appropriate method to retrieve the appropriate SharedPreferences object: use the getPreferences() method of the Activity class for activity-level preferences or the getSharedPreferences() method of the Context class for application- level preferences. Once you have a valid SharedPreferences object, you must use a SharedPreferences.Editor to add, modify, or delete preference content. To retrieve an Editor for a specific SharedPreferences object, use its edit() method.

15 Make any changes to the preferences using the methods available in the Editor class.
For example, the SharedPreferences.Editor class has helper methods for saving preferences of different data types: Store boolean values with the putBoolean() method Store float values with the putFloat() method Store int values with the putInt() method Store long values with the putLong() method Store String values with the putString() method

16 Within the Editor, you can also remove a specific preference by name using the remove() method, or remove all preferences within the set using the clear() method. Once you’ve finished editing the SharedPreferences object, you can save your changes using the Editor’s commit() method. For example, the following code retrieves a set of application preferences called “MyGamePreferences” and adds a string preference called “UserName” with a value of “Guest123” and a Boolean preference called “PaidUser” with a value of false.

17 Example Code

18 Updating Preferences Updating preferences is as simple as retrieving another SharedPreferences.Editor and making changes to a given preference by name. For example, the following code modifies the “PaidUser” preference:

19 Retrieving Preferences
You don’t need an Editor to simply read preferences. Instead, retrieve the SharedPreferences object and use the appropriate method to retrieve a preference by name: Retrieve boolean values with the getBoolean() method Retrieve float values with the getFloat() method Retrieve int values with the getInt() method Retrieve long values with the getLong() method Retrieve String values with the getString() method Each of these methods has two parameters: the preference key string and a default value to return if the preference is undefined. You can also check for the existence of a preference by name using the contains() method. You can also iterate through all preferences for a given set using the getAll() method of the SharedPreferences class.

20 Shared Preference Change Listeners
The onSharedPreferenceChangeListener is a useful class that can be implemented to invoke a callback whenever a particular Shared Preference value is added, removed, or modified. This is particularly useful for Activities and Services that use the Shared Preference framework to set application preferences. Using this handler your application components can listen for changes to user preferences and update their UIs or behavior as required. Register Shared Preference Change Listeners using the Shared Preference you want to monitor.


Download ppt "Preference Activity class"

Similar presentations


Ads by Google