Preference Activity class

Slides:



Advertisements
Similar presentations
Samsung Smart TV is a web-based application running on an application engine installed on digital TVs connected to the Internet.
Advertisements

Cosc 5/4730 Android: “Dynamic” data.. Saving Dynamic data. While there are a lot of ways to save data – Via the filesystem, database, etc. You can also.
 data/data-storage.html#pref data/data-storage.html#pref 
The Android Activity Lifecycle. Slide 2 Introduction Working with the Android logging system Rotation and multiple layouts Understanding the Android activity.
CVEV 118/698 Visual Basic Lecture 3 Prof. Mounir Mabsout Elsa Sulukdjian Walid El Asmar.
Android 4: Creating Contents Kirk Scott 1. Outline 4.1 Planning Contents 4.2 GIMP and Free Sound Recorder 4.3 Using FlashCardMaker to Create an XML File.
Emerging Platform#4: Android Bina Ramamurthy.  Android is an Operating system.  Android is an emerging platform for mobile devices.  Initially developed.
Data Access Patterns. Motivation Most software systems require persistent data (i.e. data that persists between program executions). In general, distributing.
Data Storage: Part 1 (Preferences)
 Understanding an activity  Starting an activity  Passing information between activities  Understanding intents  Understanding the activity lifecycle.
ANDROID UI – FRAGMENTS. Fragment  An activity is a container for views  When you have a larger screen device than a phone –like a tablet it can look.
Favorite Twitter® Searches App Android How to Program © by Pearson Education, Inc. All Rights Reserved.
Tip Calculator App Building an Android App with Java © by Pearson Education, Inc. All Rights Reserved.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
London April 2005 London April 2005 Creating Eyeblaster Ads The Rich Media Platform The Rich Media Platform Eyeblaster.
Virtual techdays INDIA │ Nov 2010 Developing Office Biz Application using WPF on Windows 7 Sarang Datye │ Sr. Consultant, Microsoft Sridhar Poduri.
Rajab Davudov. Agenda Eclipse, ADT and Android SDK APK file Fundamentals – Activity – Service – Content Provider – Broadcast Receiver – Intent Hello World.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Tutorial 111 The Visual Studio.NET Environment The major differences between Visual Basic 6.0 and Visual Basic.NET are the latter’s support for true object-oriented.
INTRODUCTION TO ANDROID. Slide 2 Application Components An Android application is made of up one or more of the following components Activities We will.
INTRODUCTION TO HTML5 Using jQuery with HTML5. Introducing jQuery  Although it is not a part of any W3C or WHATWG specification, jQuery performs an important.
Nilesh Singh Local Data Storage option Android provides several options for you to save persistent application data. - Shared preferences - Creation.
Data persistence How to save data using SharedPreferences, Files, and SQLite database 1Data persistence.
Android – Fragments L. Grewe.
Data Persistence Nasrullah Niazi. Share Preferences The SharedPreferences class provides a general framework that allows you to save and retrieve persistent.
Creating an Example Android App in Android Studio Activity lifecycle & UI Resources.
CS378 - Mobile Computing Persistence. Saving State We have already seen saving app state into a Bundle on orientation changes or when an app is killed.
Chapter 5 Introduction to Defining Classes
Applications with Multiple Activities. Most applications will have more than one activity. The main activity is started when the application is started.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Creating multiple views Introduction to intents Passing data to.
ANDROID AND MODEL / VIEW / CONTROLLER. Slide 2 Design Patters Common solutions to programming problems are called design patterns Design patterns are.
Android: “Dynamic” data and Preferences data.
© 2016 Cengage Learning®. May not be scanned, copied or duplicated, or posted to a publicly accessible website, in whole or in part. Android Boot Camp.
CHAPTER 9 File Storage Shared Preferences SQLite.
The Ingredients of Android Applications. A simple application in a process In a classical programming environment, the OS would load the program code.
Mobile Application Development Data Storage. Android provides several options for you to save persistent application data. The solution you choose depends.
Java for android Development Nasrullah Khan. Using instanceof in Android Development the classes such as Button, TextView, and CheckBox, which represent.
INTRODUCTION TO ANDROID. Slide 2 Introduction I take a top-down approach to describing an application’s anatomy.
Working with Multiple Activities. Slide 2 Introduction Working with multiple activities Putting together the AndroidManifest.xml file Creating multiple.
Chapter 5 Introduction to Defining Classes Fundamentals of Java.
Editing a Twitter search. Viewing search results in a browser.
Menus. Menus are a common user interface component in many types of applications. The options menu is the primary collection of menu items for an activity.
Java FX: Scene Builder.
Reactive Android Development
Mobile Applications (Android Programming)
Chapter 5: Enhancing Classes
Mobile Applications (Android Programming)
Android Application Data Storage 1.
Instructor: Mazhar Hussain
Android – Event Handling
Java Beans Sagun Dhakhwa.
Mobile Application Development Chapter 5 [Persistent Data in Android]
Activities and Intents
Mobile Application Development Chapter 4 [Android Navigation and Interface Design] IT448-Fall 2017 IT448- Fall2017.
Android 15: Settings Kirk Scott.
Using Procedures and Exception Handling
ANDROID UI – FRAGMENTS UNIT II.
Tutorial 19 - Microwave Oven Application Building Your Own Classes and Objects Outline Test-Driving the Microwave Oven Application Designing.
Phil Tayco Slide version 1.0 Created Oct 9, 2017
Making Procedural Methods
Cannon Game App Android How to Program
Android: Preferences and user settings data
Developing a Model-View-Controller Component for Joomla Part 3
CIS16 Application Development and Programming using Visual Basic.net
Activities and Intents
Lesson 23 Getting Started with Access Essentials
Designing a class.
Emerging Platform#3 Android & Programming an App
Use Case Analysis – continued
Mobile Programming Dr. Mohsin Ali Memon.
Presentation transcript:

Preference Activity class

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

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.

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

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

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>

Sharing Preferences

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 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

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, 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 Application-level preferences are available across all activities. These preferences are retrieved using the application Context class method called getSharedPreferences() by name. For example:

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 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 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.

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

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.

Example Code

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:

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.

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.