Today we start learning about settings and preferences in Android.
Settings can be an important part of some Android applications. They allow users to change the behavior of the application using a list of components that let the user control their preferences.
There is a native way of providing modifiable settings for your application using the Android's Preference APIs.
To create a settings Activity for your application, you don't need to display and handle all the Views on your own. The whole process can be done by providing an XML file which is built using subclasses of the Preference class.
Each Preference is an item in a list and provides an approporiate native way to modify a setting. Each Preference that you add has a key-value pair that is used to save the respective setting in a SharedPreferences file for your application's settings. The file is used to read the setting values in your application to modify its behaviour.
The values stored in that file for each setting can be one of the following types: Boolean, Float, Int, Long, String or Set of Strings.
The class used for building the settings Activity is PreferenceActivity. It is supported by all Android versions. There is another way to display the settings screen for Android versions 3.0 and higher - that is to create a normal Activity and use a PreferenceFragment inside of it that displays the settings.
As I said before, each setting in the application is represented by an instance of a Preferences subclass. Some of the most commonly used ones are CheckBoxPreference, ListPreference and EditTextPreference.
The CheckBoxPreference component is an item with a checkbox and works like a switch - possible values are true and false, so the data type is Boolean.
The ListPreference class is an item that opens a dialog with a list of radio buttons that the user can select. The saved data type can be any type listed above.
The EditTextPreference preference opens a dialog that lets the user input their text, saved data type is a String.
And that will be all for today. Next time we'll try creating a preference screen ourselves.
Thanks for reading!