Preference Activity class Understanding the preference Activity class

  • Slides: 20
Download presentation
Preference Activity class

Preference Activity class

Understanding the “preference. Activity” class The responsibility of the Preference. Activity class is to

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

When preferences are edited, they are stored using an instance of Shared. Preferences. The

When preferences are edited, they are stored using an instance of Shared. Preferences. The Shared. Preferences class is an interface for accessing and modifying preference data returned by get. Shared. Preferences()from any Context object. A Preference. Activity is a base class that is very similar to the Activity base class. However, the Preference. Activity behaves a bit differently. One of the most important features that the Preference. Activity 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.

Working with preferennceactivity class The preference. Activity shows a hiarchy of preferences on the

Working with preferennceactivity class The preference. Activity 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 Shared. Prefernces. 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 Preference. Screen you just built , add an activity that derives from Preference. Activity to your application. The Code for this file shown in the next slide

1. The Task. Preference class file is defined by inheriting from Preference. Activity base

1. The Task. Preference class file is defined by inheriting from Preference. Activity base class. 2. The call to add. Preference. From. Resource() method is provided with resource ID of the task_preferences. xml file stored in the res/xml directory. 3. Retrieving the Edit. Text. Preference for the default task reminder time by calling the find. Preference() method and providing it with the key that was defined in the task_preferences. xml file. 4. On this line I am obtaining the Edit. Text object, which a child of the Edit. Text. Preference, using the get. Edit. Text() 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 set. Key. Listner() method, and by providing it with an instance of Digits. Key. Listener, the Edit. Text. Preference only allows digits to be typed into the Edit. Text. Preference for the default reminder time. This is to ensure that the only values passed into the prefences are digits

At this point the activity is ready to be used , This Preference. Activity

At this point the activity is ready to be used , This Preference. Activity 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=". Task. Preferences" android: label="My Preferences"> </activity>

Sharing Preferences

Sharing Preferences

What Is A Preference? Shared preferences are simply sets of data values that are

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.

For example, your application might want to store the user’s name. The application could

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 “User. Name” ◦ The value would be the actual username string, such as “Android. Power. User 123” or “Bob” A preference can be any of a number of different data types. The following data types are supported by the Shared. Preferences class: ◦ ◦ ◦ Boolean values Float values Integer values Long values String values

How Shared Preferences Work The Android SDK includes helpful classes for getting application preferences

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 Shared. Preferences 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, email, 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.

Application-level preferences are available across all activities. These preferences are retrieved using the application

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

There is no limit to the number of sets of shared preferences your application

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:

An activity can also have private preferences. These preferences are only available within the

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:

Setting Preferences Saving preferences to your application is fairly straightforward. First, you must decide

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 Shared. Preferences object: use the get. Preferences() method of the Activity class for activity-level preferences or the get. Shared. Preferences() method of the Context class for applicationlevel preferences. Once you have a valid Shared. Preferences object, you must use a Shared. Preferences. Editor to add, modify, or delete preference content. To retrieve an Editor for a specific Shared. Preferences object, use its edit() method.

Make any changes to the preferences using the methods available in the Editor class.

Make any changes to the preferences using the methods available in the Editor class. For example, the Shared. Preferences. Editor class has helper methods for saving preferences of different data types: ◦ ◦ ◦ Store boolean values with the put. Boolean() method Store float values with the put. Float() method Store int values with the put. Int() method Store long values with the put. Long() method Store String values with the put. String() method

Within the Editor, you can also remove a specific preference by name using the

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 Shared. Preferences object, you can save your changes using the Editor’s commit() method. For example, the following code retrieves a set of application preferences called “My. Game. Preferences” and adds a string preference called “User. Name” with a value of “Guest 123” and a Boolean preference called “Paid. User” with a value of false.

Example Code

Example Code

Updating Preferences Updating preferences is as simple as retrieving another Shared. Preferences. Editor and

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

Retrieving Preferences You don’t need an Editor to simply read preferences. Instead, retrieve the

Retrieving Preferences You don’t need an Editor to simply read preferences. Instead, retrieve the Shared. Preferences object and use the appropriate method to retrieve a preference by name: ◦ ◦ ◦ Retrieve boolean values with the get. Boolean() method Retrieve float values with the get. Float() method Retrieve int values with the get. Int() method Retrieve long values with the get. Long() method Retrieve String values with the get. String() 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 get. All() method of the Shared. Preferences class.

Shared Preference Change Listeners The on. Shared. Preference. Change. Listener is a useful class

Shared Preference Change Listeners The on. Shared. Preference. Change. Listener 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.