 When developing mobile applications, it is often necessary to store the settings of the application (the color theme chosen by the user, the password to access the application, the synchronization settings with the server, etc.). Since such tasks occur quite often, Google took care of the developers and created a mechanism for quick and easy implementation of this functionality. This mechanism allows you to easily create, save, display and produce various manipulations with the settings of the application. Moreover, it allows you to automatically create a user interface. When describing the type of the settings parameter, a part of the user interface is automatically generated depending on the type of the parameter (boolean - checkbox, String - input field, ...) without writing code. Sounds good for fast development, right?
 When developing mobile applications, it is often necessary to store the settings of the application (the color theme chosen by the user, the password to access the application, the synchronization settings with the server, etc.). Since such tasks occur quite often, Google took care of the developers and created a mechanism for quick and easy implementation of this functionality. This mechanism allows you to easily create, save, display and produce various manipulations with the settings of the application. Moreover, it allows you to automatically create a user interface. When describing the type of the settings parameter, a part of the user interface is automatically generated depending on the type of the parameter (boolean - checkbox, String - input field, ...) without writing code. Sounds good for fast development, right?
- ...
- @Override
- public void onCreate ( Bundle savedInstanceState ) {
- super . onCreate ( savedInstanceState ) ;
- addPreferencesFromResource ( R. xml . preferences ) ;
- }
- ...
- <? xml version = "1.0" encoding = "utf-8" ?>
- <PreferenceScreen xmlns: android = " schemas.android.com/apk/res/android" >
- <PreferenceCategory
- android: title = "First Category"
- android: key = "first_category" >
- <CheckBoxPreference
- android: key = "perform_updates"
- android: summary = "Enable or disable data updates"
- android: title = "Enable updates"
- android: defaultValue = "true"
- />
- <ListPreference
- android: key = "updates_interval"
- android: title = "Updates interval"
- android: summary = "Define how often updates will be performed"
- android: defaultValue = "1000"
- android: entries = "@ array / updateInterval"
- android: entryValues = "@ array / updateIntervalValues"
- android: dependency = "perform_updates"
- />
- </ PreferenceCategory >
- <PreferenceCategory
- android: title = "Second Category"
- android: key = "second_category" >
- <EditTextPreference
- android: key = "welcome_message"
- android: title = "Welcome Message"
- android: summary = "Define
- android: dialogTitle = "Welcome Message"
- android: dialogMessage = "Provide a message"
- android: defaultValue = "Default welcome message" />
- </ PreferenceCategory >
- </ PreferenceScreen >
- <? xml version = "1.0" encoding = "utf-8" ?>
- <resources >
- <string-array name = "updateInterval" >
- <item name = "1000" > Every 1 second </ item >
- <item name = "5000" > Every 5 seconds </ item >
- <item name = "30000" > Every 30 seconds </ item >
- <item name = "60000" > Every 1 minute </ item >
- <item name = "300000" > Every 5 minutes </ item >
- </ string-array >
- <string-array name = "updateIntervalValues" >
- <item name = "1000" > 1000 </ item >
- <item name = "5000" > 5000 </ item >
- <item name = "30000" > 30000 </ item >
- <item name = "60000" > 60000 </ item >
- <item name = "300000" > 300000 </ item >
- </ string-array >
- </ resources >
- <EditTextPreference
- android: key = "pin"
- android: title = "PIN code"
- android: summary = "If login screen is enabled"
- android: dialogTitle = "PIN code"
- android: dialogMessage = "Provide a PIN"
- android: defaultValue = ""
- android: inputType = "number"
- android: password = "true"
- android: dependency = "runLoginScreen"
- />
- package com.javacodegeeks.android.preferences ;
- import android.content.Intent ;
- import android.os.Bundle ;
- import android.preference.PreferenceActivity ;
- import android.view.Menu ;
- import android.view.MenuItem ;
- public class QuickPrefsActivity extends PreferenceActivity {
- @Override
- public void onCreate ( Bundle savedInstanceState ) {
- super . onCreate ( savedInstanceState ) ;
- addPreferencesFromResource ( R. xml . preferences ) ;
- }
- @Override
- public boolean onCreateOptionsMenu ( Menu menu ) {
- menu. add ( Menu . NONE , 0 , 0 , "Show current settings" ) ;
- return super . onCreateOptionsMenu ( menu ) ;
- }
- @Override
- public boolean onOptionsItemSelected ( MenuItem item ) {
- switch ( item. getItemId ( ) ) {
- case 0 :
- startActivity ( new Intent ( this , ShowSettingsActivity. class ) ) ;
- return true ;
- }
- return false ;
- }
- }
- <? xml version = "1.0" encoding = "utf-8" ?>
- <manifest xmlns: android = " schemas.android.com/apk/res/android"
- package = "com.javacodegeeks.android.preferences"
- android: versionCode = "1"
- android: versionName = "1.0" >
- <application android: icon = "@ drawable / icon" android: label = "@ string / app_name" >
- <activity android: name = ".QuickPrefsActivity" android: label = "@ string / app_name" >
- <intent-filter >
- <action android: name = "android.intent.action.MAIN" />
- <category android: name = "android.intent.category.LAUNCHER" />
- </ intent-filter >
- </ activity >
- <activity android: name = ".ShowSettingsActivity" />
- </ application >
- <uses-sdk android: minSdkVersion = "3" />
- </ manifest >
- package com.javacodegeeks.android.preferences ;
- import android.app.Activity ;
- import android.content.SharedPreferences ;
- import android.os.Bundle ;
- import android.preference.PreferenceManager ;
- import android.widget.TextView ;
- public class ShowSettingsActivity extends Activity {
- @Override
- protected void onCreate ( Bundle savedInstanceState ) {
- super . onCreate ( savedInstanceState ) ;
- setContentView ( R. layout . show_settings_layout ) ;
- SharedPreferences sharedPrefs = PreferenceManager. getDefaultSharedPreferences ( this ) ;
- StringBuilder builder = new StringBuilder ( ) ;
- builder. append ( "n" + sharedPrefs. getBoolean ( "perform_updates" , false ) ) ;
- builder. append ( "n" + sharedPrefs. getString ( "updates_interval" , "-1" ) ) ;
- builder. append ( "n" + sharedPrefs. getString ( "welcome_message" , "NULL" ) ) ;
- TextView settingsTextView = ( TextView ) findViewById ( R. id . Settings_text_view ) ;
- settingsTextView. setText ( builder. toString ( ) ) ;
- }
- }
- <? xml version = "1.0" encoding = "utf-8" ?>
- <LinearLayout
- xmlns: android = " schemas.android.com/apk/res/android"
- android: orientation = "vertical"
- android: layout_width = "fill_parent"
- android: layout_height = "fill_parent"
- >
- <Textview
- android: id = "@ + id / settings_text_view"
- android: layout_width = "fill_parent"
- android: layout_height = "wrap_content"
- />
- </ LinearLayout >




Source: https://habr.com/ru/post/139330/
All Articles