Home | History | Annotate | Download | only in ui
      1 page.title=
      2 page.tags=preference,preferenceactivity,preferencefragment
      3 
      4 @jd:body
      5 
      6 
      7 <div id="qv-wrapper">
      8 <div id="qv">
      9 
     10 <h2></h2>
     11 <ol>
     12   <li><a href="#Overview"></a>
     13     <ol>
     14       <li><a href="#SettingTypes"></a></li>
     15     </ol>
     16   </li>
     17   <li><a href="#DefiningPrefs">XML </a>
     18     <ol>
     19       <li><a href="#Groups"></a></li>
     20       <li><a href="#Intents"></a></li>
     21     </ol>
     22   </li>
     23   <li><a href="#Activity"> </a></li>
     24   <li><a href="#Fragment"> </a></li>
     25   <li><a href="#Defaults"></a></li>
     26   <li><a href="#PreferenceHeaders"> </a>
     27     <ol>
     28       <li><a href="#CreateHeaders"> </a></li>
     29       <li><a href="#DisplayHeaders"></a></li>
     30       <li><a href="#BackCompatHeaders"> </a></li>
     31     </ol>
     32   </li>
     33   <li><a href="#ReadingPrefs"></a>
     34     <ol>
     35       <li><a href="#Listening"></a></li>
     36     </ol>
     37   </li>
     38   <li><a href="#NetworkUsage"></a></li>
     39   <li><a href="#Custom"> </a>
     40     <ol>
     41       <li><a href="#CustomSelected"> </a></li>
     42       <li><a href="#CustomSave"></a></li>
     43       <li><a href="#CustomInitialize"></a></li>
     44       <li><a href="#CustomDefault"></a></li>
     45       <li><a href="#CustomSaveState"></a></li>
     46     </ol>
     47   </li>
     48 </ol>
     49 
     50 <h2></h2>
     51 <ol>
     52   <li>{@link android.preference.Preference}</li>
     53   <li>{@link android.preference.PreferenceActivity}</li>
     54   <li>{@link android.preference.PreferenceFragment}</li>
     55 </ol>
     56 
     57 
     58 <h2></h2>
     59 <ol>
     60   <li><a href="{@docRoot}design/patterns/settings.html">Settings design guide</a></li>
     61 </ol>
     62 </div>
     63 </div>
     64 
     65 
     66 
     67 
     68 <p>
     69 
     70 </p>
     71 
     72 <p>Android  {@link android.preference.Preference} API  Android 
     73 
     74 {@link android.preference.Preference} API 
     75 </p>
     76 
     77 <div class="note design">
     78 <p><strong></strong></p>
     79   <p><a href="{@docRoot}design/patterns/settings.html"></a></p>
     80 </div>
     81 
     82 
     83 <img src="{@docRoot}images/ui/settings/settings.png" alt="" width="435" />
     84 <p class="img-caption"><strong> 1.</strong> Android SMS 
     85 {@link android.preference.Preference} 
     86 </p>
     87 
     88 
     89 
     90 
     91 <h2 id="Overview"></h2>
     92 
     93 <p>{@link android.view.View}  XML  {@link android.preference.Preference} 
     94 
     95 </p>
     96 
     97 <p>1  {@link android.preference.Preference} 1 
     98  {@link android.preference.Preference}  UI 
     99 {@link android.preference.CheckBoxPreference} {@link android.preference.ListPreference} 
    100 
    101 </p>
    102 
    103 <p> {@link android.preference.Preference}  {@link android.content.SharedPreferences} 
    104 
    105  {@link android.content.SharedPreferences} 
    106  {@link android.content.SharedPreferences} 
    107 
    108 </p>
    109 
    110 <p> {@link android.content.SharedPreferences} 
    111 </p>
    112 
    113 <ul>
    114   <li>Boolean</li>
    115   <li>Float</li>
    116   <li>Int</li>
    117   <li>Long</li>
    118   <li>String</li>
    119   <li>String {@link java.util.Set}</li>
    120 </ul>
    121 
    122 <p> UI  {@link android.view.View}  {@link android.preference.Preference}  {@link android.app.Activity}  {@link android.app.Fragment} 
    123 
    124 
    125 </p>
    126 
    127 <ul>
    128   <li> 3.0  AndroidAPI  10 {@link android.preference.PreferenceActivity} 
    129 </li>
    130   <li>Android 3.0  {@link android.preference.PreferenceFragment}  {@link android.app.Activity} {@link android.preference.PreferenceActivity}  2  
    131 
    132 
    133 </li>
    134 </ul>
    135 
    136 <p>{@link android.preference.PreferenceActivity}  {@link android.preference.PreferenceFragment} <a href="#Activity"> </a><a href="#Fragment"> </a>
    137 
    138 </p>
    139 
    140 
    141 <h3 id="SettingTypes"></h3>
    142 
    143 <p>{@link android.preference.Preference} 
    144 
    145  
    146  1. SMS 
    147   {@link android.preference.Preference} 
    148 </p>
    149 
    150 <p></p>
    151 
    152 <dl>
    153   <dt>{@link android.preference.CheckBoxPreference}</dt>
    154   <dd>Boolean <code>true</code>
    155 </dd>
    156 
    157   <dt>{@link android.preference.ListPreference}</dt>
    158   <dd>
    159 </dd>
    160 
    161   <dt>{@link android.preference.EditTextPreference}</dt>
    162   <dd>{@link android.widget.EditText} {@link java.lang.String} 
    163 </dd>
    164 </dl>
    165 
    166 <p>{@link android.preference.Preference} 
    167 </p>
    168 
    169 <p>
    170  {@link android.preference.Preference} 
    171  {@link android.preference.Preference} 
    172 <a href="#Custom"> </a></p>
    173 
    174 
    175 
    176 <h2 id="DefiningPrefs">XML </h2>
    177 
    178 <p> {@link android.preference.Preference} {@link android.preference.Preference}  XML 
    179 
    180 XML  XML 
    181 
    182 </p>
    183 
    184 <p> {@link android.preference.Preference} {@code &lt;CheckBoxPreference&gt;}  XML 
    185 </p>
    186 
    187 <p> XML {@code res/xml/}  XML {@code preferences.xml} 
    188  {@link android.preference.PreferenceScreen}  1 
    189 
    190 </p>
    191 
    192 <p class="note"><strong>:</strong>   XML 
    193 </p>
    194 
    195 <p>XML {@link android.preference.PreferenceScreen &lt;PreferenceScreen&gt;} 
    196  {@link android.preference.Preference} 
    197 {@link android.preference.PreferenceScreen &lt;PreferenceScreen&gt;}  1 
    198 
    199 </p>
    200 
    201 <p></p>
    202 
    203 <pre>
    204 &lt;?xml version="1.0" encoding="utf-8"?>
    205 &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    206     &lt;CheckBoxPreference
    207         android:key="pref_sync"
    208         android:title="@string/pref_sync"
    209         android:summary="@string/pref_sync_summ"
    210         android:defaultValue="true" />
    211     &lt;ListPreference
    212         android:dependency="pref_sync"
    213         android:key="pref_syncConnectionType"
    214         android:title="@string/pref_syncConnectionType"
    215         android:dialogTitle="@string/pref_syncConnectionType"
    216         android:entries="@array/pref_syncConnectionTypes_entries"
    217         android:entryValues="@array/pref_syncConnectionTypes_values"
    218         android:defaultValue="@string/pref_syncConnectionTypes_default" />
    219 &lt;/PreferenceScreen>
    220 </pre>
    221 
    222 <p>{@link android.preference.CheckBoxPreference}  {@link android.preference.ListPreference} 
    223  3 </p>
    224 
    225 <dl>
    226   <dt>{@code android:key}</dt>
    227   <dd> {@link android.content.SharedPreferences} 
    228 
    229 
    230   <p> {@link android.preference.PreferenceCategory} {@link android.preference.PreferenceScreen}  {@link android.content.Intent} <a href="#Intents">{@code &lt;intent&gt;}</a>  {@link android.app.Fragment} <a href="{@docRoot}reference/android/preference/Preference.html#attr_android:fragment">{@code android:fragment}</a> <em></em>
    231 
    232 
    233 </p>
    234   </dd>
    235   <dt>{@code android:title}</dt>
    236   <dd></dd>
    237   <dt>{@code android:defaultValue}</dt>
    238   <dd> {@link android.content.SharedPreferences} 
    239 
    240 </dd>
    241 </dl>
    242 
    243 <p>{@link android.preference.Preference}
    244 </p>
    245 
    246 
    247 <div class="figure" style="width:300px">
    248   <img src="{@docRoot}images/ui/settings/settings-titles.png" alt="" />
    249   <p class="img-caption"><strong> 2.</strong> 
    250  <br/><b>1.</b>{@link android.preference.PreferenceCategory &lt;PreferenceCategory&gt;} 
    251  <br/><b>2.</b>{@code android:title} 
    252 </p>
    253 </div>
    254 
    255 
    256 <p> 10 
    257 
    258 </p>
    259 
    260 
    261 <h3 id="Groups"></h3>
    262 
    263 <p>10 
    264 1 
    265 
    266  2 </p>
    267 
    268 <ul>
    269   <li><a href="#Titles"></a></li>
    270   <li><a href="#Subscreens"></a></li>
    271 </ul>
    272 
    273 <p> 1 Android Design <a href="{@docRoot}design/patterns/settings.html">Settings</a> 
    274 
    275 </p>
    276 
    277 
    278 <h4 id="Titles"></h4>
    279 
    280 <p> 2. {@link android.preference.Preference}  1  {@link android.preference.PreferenceCategory} 
    281 
    282 </p>
    283 
    284 <p></p>
    285 
    286 <pre>
    287 &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    288     &lt;PreferenceCategory
    289         android:title="&#64;string/pref_sms_storage_title"
    290         android:key="pref_key_storage_settings">
    291         &lt;CheckBoxPreference
    292             android:key="pref_key_auto_delete"
    293             android:summary="&#64;string/pref_summary_auto_delete"
    294             android:title="&#64;string/pref_title_auto_delete"
    295             android:defaultValue="false"... />
    296         &lt;Preference
    297             android:key="pref_key_sms_delete_limit"
    298             android:dependency="pref_key_auto_delete"
    299             android:summary="&#64;string/pref_summary_delete_limit"
    300             android:title="&#64;string/pref_title_sms_delete"... />
    301         &lt;Preference
    302             android:key="pref_key_mms_delete_limit"
    303             android:dependency="pref_key_auto_delete"
    304             android:summary="&#64;string/pref_summary_delete_limit"
    305             android:title="&#64;string/pref_title_mms_delete" ... />
    306     &lt;/PreferenceCategory>
    307     ...
    308 &lt;/PreferenceScreen>
    309 </pre>
    310 
    311 
    312 <h4 id="Subscreens"></h4>
    313 
    314 <p> 3. {@link android.preference.Preference}  {@link android.preference.PreferenceScreen} 
    315 
    316 </p>
    317 
    318 <img src="{@docRoot}images/ui/settings/settings-subscreen.png" alt="" />
    319 <p class="img-caption"><strong> 3.</strong> {@code &lt;PreferenceScreen&gt;} 
    320 
    321 </p>
    322 
    323 <p></p>
    324 
    325 <pre>
    326 &lt;PreferenceScreen  xmlns:android="http://schemas.android.com/apk/res/android">
    327     &lt;!-- opens a subscreen of settings -->
    328     &lt;PreferenceScreen
    329         android:key="button_voicemail_category_key"
    330         android:title="&#64;string/voicemail"
    331         android:persistent="false">
    332         &lt;ListPreference
    333             android:key="button_voicemail_provider_key"
    334             android:title="&#64;string/voicemail_provider" ... />
    335         &lt;!-- opens another nested subscreen -->
    336         &lt;PreferenceScreen
    337             android:key="button_voicemail_setting_key"
    338             android:title="&#64;string/voicemail_settings"
    339             android:persistent="false">
    340             ...
    341         &lt;/PreferenceScreen>
    342         &lt;RingtonePreference
    343             android:key="button_voicemail_ringtone_key"
    344             android:title="&#64;string/voicemail_ringtone_title"
    345             android:ringtoneType="notification" ... />
    346         ...
    347     &lt;/PreferenceScreen>
    348     ...
    349 &lt;/PreferenceScreen>
    350 </pre>
    351 
    352 
    353 <h3 id="Intents"></h3>
    354 
    355 <p> 
    356   {@link android.content.Intent}  {@code &lt;Preference&gt;}  {@code &lt;intent&gt;} 
    357 
    358 </p>
    359 
    360 <p> </p>
    361 
    362 <pre>
    363 &lt;Preference android:title="@string/prefs_web_page" >
    364     &lt;intent android:action="android.intent.action.VIEW"
    365             android:data="http://www.example.com" />
    366 &lt;/Preference>
    367 </pre>
    368 
    369 <p></p>
    370 
    371 <dl>
    372   <dt>{@code android:action}</dt>
    373     <dd>{@link android.content.Intent#setAction setAction()} 
    374 </dd>
    375   <dt>{@code android:data}</dt>
    376     <dd>{@link android.content.Intent#setData setData()} </dd>
    377   <dt>{@code android:mimeType}</dt>
    378     <dd>{@link android.content.Intent#setType setType()}  MIME 
    379 </dd>
    380   <dt>{@code android:targetClass}</dt>
    381     <dd>{@link android.content.Intent#setComponent setComponent()} 
    382 </dd>
    383   <dt>{@code android:targetPackage}</dt>
    384     <dd>{@link android.content.Intent#setComponent setComponent()} 
    385 </dd>
    386 </dl>
    387 
    388 
    389 
    390 <h2 id="Activity"> </h2>
    391 
    392 <p>{@link android.preference.PreferenceActivity} 
    393 {@link android.preference.Preference}  {@link android.app.Activity} 
    394 
    395 {@link android.preference.PreferenceActivity}  {@link android.preference.Preference} 
    396 
    397 </p>
    398 
    399 <p class="note"><strong>:</strong> Android 3.0  {@link android.preference.PreferenceFragment} 
    400 <a href="#Fragment"> </a>
    401 </p>
    402 
    403 <p>{@link android.preference.PreferenceActivity#onCreate onCreate()} 
    404  {@link android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()} XML 
    405 
    406 {@link android.preference.PreferenceActivity} 
    407 </p>
    408 
    409 <pre>
    410 public class SettingsActivity extends PreferenceActivity {
    411     &#64;Override
    412     public void onCreate(Bundle savedInstanceState) {
    413         super.onCreate(savedInstanceState);
    414         addPreferencesFromResource(R.xml.preferences);
    415     }
    416 }
    417 </pre>
    418 
    419 <p>  {@link android.content.SharedPreferences} 
    420 
    421 {@link android.content.SharedPreferences} <a href="#ReadingPrefs"></a>
    422 
    423 
    424 </p>
    425 
    426 
    427 
    428 
    429 <h2 id="Fragment"> </h2>
    430 
    431 <p>Android 3.0API  11{@link android.preference.PreferenceFragment}  {@link android.preference.Preference} 
    432 
    433 {@link android.preference.PreferenceFragment}  &mdash; {@link android.preference.PreferenceActivity} 
    434 </p>
    435 
    436 <p><a href="{@docRoot}guide/components/fragments.html"></a>
    437 
    438  {@link android.preference.PreferenceActivity} {@link android.preference.PreferenceFragment} 
    439 
    440 </p>
    441 
    442 <p>{@link android.preference.PreferenceFragment} {@link android.preference.PreferenceFragment#onCreate onCreate()}  {@link android.preference.PreferenceFragment#addPreferencesFromResource addPreferencesFromResource()}  
    443 
    444 
    445 </p>
    446 
    447 <pre>
    448 public static class SettingsFragment extends PreferenceFragment {
    449     &#64;Override
    450     public void onCreate(Bundle savedInstanceState) {
    451         super.onCreate(savedInstanceState);
    452 
    453         // Load the preferences from an XML resource
    454         addPreferencesFromResource(R.xml.preferences);
    455     }
    456     ...
    457 }
    458 </pre>
    459 
    460 <p> {@link android.app.Fragment}  {@link android.app.Activity} 
    461 </p>
    462 
    463 <pre>
    464 public class SettingsActivity extends Activity {
    465     &#64;Override
    466     protected void onCreate(Bundle savedInstanceState) {
    467         super.onCreate(savedInstanceState);
    468 
    469         // Display the fragment as the main content.
    470         getFragmentManager().beginTransaction()
    471                 .replace(android.R.id.content, new SettingsFragment())
    472                 .commit();
    473     }
    474 }
    475 </pre>
    476 
    477 <p class="note"><strong>:</strong> {@link android.preference.PreferenceFragment}  {@link android.content.Context} 
    478 {@link android.content.Context} {@link android.app.Fragment#getActivity()} 
    479 {@link android.app.Fragment#getActivity()} 
    480 {@link android.app.Fragment#getActivity()}  null 
    481 
    482 </p>
    483 
    484 
    485 <h2 id="Defaults"></h2>
    486 
    487 <p> {@link android.preference.Preference}  {@link android.content.SharedPreferences} 
    488 
    489 
    490 </p>
    491 
    492 <p>{@code android:defaultValue} XML  {@link android.preference.Preference} 
    493 
    494  {@link android.preference.Preference} 
    495 
    496 </p>
    497 
    498 <pre>
    499 &lt;!-- default value is a boolean -->
    500 &lt;CheckBoxPreference
    501     android:defaultValue="true"
    502     ... />
    503 
    504 &lt;!-- default value is a string -->
    505 &lt;ListPreference
    506     android:defaultValue="@string/pref_syncConnectionTypes_default"
    507     ... />
    508 </pre>
    509 
    510 <p>  {@link android.app.Activity#onCreate onCreate()} {@link android.preference.PreferenceManager#setDefaultValues setDefaultValues()} 
    511 
    512 
    513 </p>
    514 
    515 <pre>
    516 PreferenceManager.setDefaultValues(this, R.xml.advanced_preferences, false);
    517 </pre>
    518 
    519 <p>{@link android.app.Activity#onCreate onCreate()}  
    520 
    521 
    522 </p>
    523 
    524 <p> 3 </p>
    525 <ul>
    526   <li> {@link android.content.Context}</li>
    527   <li> XML  ID</li>
    528   <li>
    529 <p><code>false</code>  1  {@link android.preference.PreferenceManager#KEY_HAS_SET_DEFAULT_VALUES}  false 
    530 
    531 </p></li>
    532 </ul>
    533 
    534 <p> 3  <code>false</code> 
    535 
    536  <code>true</code> 
    537 </p>
    538 
    539 
    540 
    541 <h2 id="PreferenceHeaders"> </h2>
    542 
    543 <p><a href="#Subscreens"></a> 4. . 5 
    544 
    545  Android 3.0  {@link android.preference.PreferenceScreen} Android 3.0 
    546 
    547 </p>
    548 
    549 <p></p>
    550 <ol>
    551   <li> {@link android.preference.PreferenceFragment} 
    552  XML 
    553 </li>
    554   <li> XML  
    555 </li>
    556   <li>{@link android.preference.PreferenceActivity} </li>
    557   <li>{@link android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()}  
    558 
    559 </li>
    560 </ol>
    561 
    562 <p>{@link android.preference.PreferenceActivity} . 4  2  
    563 </p>
    564 
    565 <p> 3.0  Android 3.0 {@link android.preference.PreferenceFragment}  3.0  2 <a href="#BackCompatHeaders"> </a>
    566 
    567 
    568 
    569 </p>
    570 
    571 <img src="{@docRoot}images/ui/settings/settings-headers-tablet.png" alt="" />
    572 <p class="img-caption"><strong> 4.</strong> 2   <br/><b>1.</b>XML  
    573  <br/><b>2.</b>XML   {@code &lt;header&gt;}  {@link android.preference.PreferenceFragment} 
    574 
    575 </p>
    576 
    577 <img src="{@docRoot}images/ui/settings/settings-headers-handset.png" alt="" />
    578 <p class="img-caption"><strong> 5.</strong>  {@link android.preference.PreferenceFragment} 
    579 
    580 </p>
    581 
    582 
    583 <h3 id="CreateHeaders" style="clear:left"> </h3>
    584 
    585 <p> {@code &lt;preference-headers&gt;}  {@code &lt;header&gt;} 
    586 </p>
    587 
    588 <pre>
    589 &lt;?xml version="1.0" encoding="utf-8"?>
    590 &lt;preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    591     &lt;header
    592         android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentOne"
    593         android:title="@string/prefs_category_one"
    594         android:summary="@string/prefs_summ_category_one" />
    595     &lt;header
    596         android:fragment="com.example.prefs.SettingsActivity$SettingsFragmentTwo"
    597         android:title="@string/prefs_category_two"
    598         android:summary="@string/prefs_summ_category_two" >
    599         &lt;!-- key/value pairs can be included as arguments for the fragment. -->
    600         &lt;extra android:name="someKey" android:value="someHeaderValue" />
    601     &lt;/header>
    602 &lt;/preference-headers>
    603 </pre>
    604 
    605 <p>{@code android:fragment}  {@link android.preference.PreferenceFragment} 
    606 </p>
    607 
    608 <p>{@code &lt;extras&gt;}  {@link android.os.Bundle} 
    609 {@link android.app.Fragment#getArguments()} 
    610  {@link android.preference.PreferenceFragment}  XML  1 
    611 
    612 
    613 </p>
    614 
    615 <p> {@code "settings"}  {@code &lt;extra&gt;} 
    616 </p>
    617 
    618 <pre>
    619 public static class SettingsFragment extends PreferenceFragment {
    620     &#64;Override
    621     public void onCreate(Bundle savedInstanceState) {
    622         super.onCreate(savedInstanceState);
    623 
    624         String settings = getArguments().getString("settings");
    625         if ("notifications".equals(settings)) {
    626             addPreferencesFromResource(R.xml.settings_wifi);
    627         } else if ("sync".equals(settings)) {
    628             addPreferencesFromResource(R.xml.settings_sync);
    629         }
    630     }
    631 }
    632 </pre>
    633 
    634 
    635 
    636 <h3 id="DisplayHeaders"></h3>
    637 
    638 <p> {@link android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()} {@link android.preference.PreferenceActivity#loadHeadersFromResource loadHeadersFromResource()} 
    639 
    640 
    641 </p>
    642 
    643 <pre>
    644 public class SettingsActivity extends PreferenceActivity {
    645     &#64;Override
    646     public void onBuildHeaders(List&lt;Header> target) {
    647         loadHeadersFromResource(R.xml.preference_headers, target);
    648     }
    649 }
    650 </pre>
    651 
    652 <p> {@link android.preference.PreferenceFragment} 
    653 </p>
    654 
    655 <p class="note"><strong>:</strong>  {@link android.preference.PreferenceActivity}  {@link android.preference.PreferenceActivity#onCreate onCreate()} 
    656 
    657 
    658 </p>
    659 
    660 
    661 <h3 id="BackCompatHeaders"> </h3>
    662 
    663 <p> Android 3.0 3.0  2  
    664 3.0  Android   {@link android.preference.Preference &lt;Preference&gt;}  XML 
    665 
    666 
    667 </p>
    668 
    669 <p> {@link android.preference.PreferenceScreen}  {@link android.preference.Preference &lt;Preference&gt;}  XML  {@link android.preference.PreferenceActivity}  {@link android.content.Intent} 
    670 
    671 
    672 </p>
    673 
    674 <p> Android 3.0   XML {@code res/xml/preference_headers.xml}
    675 </p>
    676 
    677 <pre>
    678 &lt;preference-headers xmlns:android="http://schemas.android.com/apk/res/android">
    679     &lt;header
    680         android:fragment="com.example.prefs.SettingsFragmentOne"
    681         android:title="@string/prefs_category_one"
    682         android:summary="@string/prefs_summ_category_one" />
    683     &lt;header
    684         android:fragment="com.example.prefs.SettingsFragmentTwo"
    685         android:title="@string/prefs_category_two"
    686         android:summary="@string/prefs_summ_category_two" />
    687 &lt;/preference-headers>
    688 </pre>
    689 
    690 <p> Android 3.0  {@code res/xml/preference_headers_legacy.xml}
    691 </p>
    692 
    693 <pre>
    694 &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
    695     &lt;Preference
    696         android:title="@string/prefs_category_one"
    697         android:summary="@string/prefs_summ_category_one"  >
    698         &lt;intent
    699             android:targetPackage="com.example.prefs"
    700             android:targetClass="com.example.prefs.SettingsActivity"
    701             android:action="com.example.prefs.PREFS_ONE" />
    702     &lt;/Preference>
    703     &lt;Preference
    704         android:title="@string/prefs_category_two"
    705         android:summary="@string/prefs_summ_category_two" >
    706         &lt;intent
    707             android:targetPackage="com.example.prefs"
    708             android:targetClass="com.example.prefs.SettingsActivity"
    709             android:action="com.example.prefs.PREFS_TWO" />
    710     &lt;/Preference>
    711 &lt;/PreferenceScreen>
    712 </pre>
    713 
    714 <p>{@code &lt;preference-headers&gt;}  Android 3.0 Android 3.0  {@link android.preference.PreferenceActivity}  {@link android.preference.PreferenceActivity#onBuildHeaders onBuildHeaders()} 
    715 
    716  {@code preference_headers_legacy.xml}Android Android 3.0{@link android.os.Build.VERSION_CODES#HONEYCOMB}{@link android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()} 
    717 
    718 
    719 
    720 
    721 </p>
    722 
    723 <pre>
    724 &#64;Override
    725 public void onCreate(Bundle savedInstanceState) {
    726     super.onCreate(savedInstanceState);
    727     ...
    728 
    729     if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.HONEYCOMB) {
    730         // Load the legacy preferences headers
    731         addPreferencesFromResource(R.xml.preference_headers_legacy);
    732     }
    733 }
    734 
    735 // Called only on Honeycomb and later
    736 &#64;Override
    737 public void onBuildHeaders(List&lt;Header> target) {
    738    loadHeadersFromResource(R.xml.preference_headers, target);
    739 }
    740 </pre>
    741 
    742 <p> {@link android.content.Intent}  
    743  XML  {@code &lt;intent&gt;} 
    744 </p>
    745 
    746 <pre>
    747 final static String ACTION_PREFS_ONE = "com.example.prefs.PREFS_ONE";
    748 ...
    749 
    750 &#64;Override
    751 public void onCreate(Bundle savedInstanceState) {
    752     super.onCreate(savedInstanceState);
    753 
    754     String action = getIntent().getAction();
    755     if (action != null &amp;&amp; action.equals(ACTION_PREFS_ONE)) {
    756         addPreferencesFromResource(R.xml.preferences);
    757     }
    758     ...
    759 
    760     else if (Build.VERSION.SDK_INT &lt; Build.VERSION_CODES.HONEYCOMB) {
    761         // Load the legacy preferences headers
    762         addPreferencesFromResource(R.xml.preference_headers_legacy);
    763     }
    764 }
    765 </pre>
    766 
    767 <p>{@link android.preference.PreferenceActivity#addPreferencesFromResource addPreferencesFromResource()}  1 else if 1 
    768 
    769 
    770 </p>
    771 
    772 
    773 
    774 
    775 
    776 <h2 id="ReadingPrefs"></h2>
    777 
    778 <p> {@link android.preference.PreferenceManager#getDefaultSharedPreferences PreferenceManager.getDefaultSharedPreferences()} 
    779 
    780 
    781 {@link android.preference.PreferenceActivity}  {@link android.preference.Preference}  {@link android.content.SharedPreferences} 
    782 
    783 
    784 </p>
    785 
    786 <p> 1 
    787 </p>
    788 
    789 <pre>
    790 SharedPreferences sharedPref = PreferenceManager.getDefaultSharedPreferences(this);
    791 String syncConnPref = sharedPref.getString(SettingsActivity.KEY_PREF_SYNC_CONN, "");
    792 </pre>
    793 
    794 
    795 
    796 <h3 id="Listening"></h3>
    797 
    798 <p>
    799  1  {@link android.content.SharedPreferences.OnSharedPreferenceChangeListener SharedPreference.OnSharedPreferenceChangeListener} {@link android.content.SharedPreferences#registerOnSharedPreferenceChangeListener registerOnSharedPreferenceChangeListener()}  {@link android.content.SharedPreferences} 
    800 
    801 
    802 
    803 
    804 </p>
    805 
    806 <p>  {@link android.content.SharedPreferences.OnSharedPreferenceChangeListener#onSharedPreferenceChanged onSharedPreferenceChanged()} 1 
    807 
    808 
    809 </p>
    810 
    811 <pre>
    812 public class SettingsActivity extends PreferenceActivity
    813                               implements OnSharedPreferenceChangeListener {
    814     public static final String KEY_PREF_SYNC_CONN = "pref_syncConnectionType";
    815     ...
    816 
    817     public void onSharedPreferenceChanged(SharedPreferences sharedPreferences,
    818         String key) {
    819         if (key.equals(KEY_PREF_SYNC_CONN)) {
    820             Preference connectionPref = findPreference(key);
    821             // Set summary to be the user-description for the selected value
    822             connectionPref.setSummary(sharedPreferences.getString(key, ""));
    823         }
    824     }
    825 }
    826 </pre>
    827 
    828 <p>  {@link android.preference.PreferenceActivity#findPreference findPreference()}  {@link android.preference.Preference} 
    829 
    830 
    831  {@link android.preference.ListPreference}  {@link android.preference.Preference#setSummary setSummary()}  5. 
    832 
    833 
    834 </p>
    835 
    836 <p class="note"><strong>:</strong> Android Design  <a href="{@docRoot}design/patterns/settings.html">Settings</a> {@link android.preference.ListPreference} 
    837 
    838 </p>
    839 
    840 <p>{@link android.app.Activity#onResume}  {@link android.app.Activity#onPause} {@link android.content.SharedPreferences.OnSharedPreferenceChangeListener} 
    841 
    842 </p>
    843 
    844 <pre>
    845 &#64;Override
    846 protected void onResume() {
    847     super.onResume();
    848     getPreferenceScreen().getSharedPreferences()
    849             .registerOnSharedPreferenceChangeListener(this);
    850 }
    851 
    852 &#64;Override
    853 protected void onPause() {
    854     super.onPause();
    855     getPreferenceScreen().getSharedPreferences()
    856             .unregisterOnSharedPreferenceChangeListener(this);
    857 }
    858 </pre>
    859 
    860 <p class="caution"><strong>:</strong> {@link android.content.SharedPreferences#registerOnSharedPreferenceChangeListener registerOnSharedPreferenceChangeListener()}  
    861 
    862 
    863  
    864  
    865 
    866 </p>
    867 
    868 <p>
    869  
    870 </p>
    871 
    872 <pre>
    873 prefs.registerOnSharedPreferenceChangeListener(
    874   // Bad! The listener is subject to garbage collection!
    875   new SharedPreferences.OnSharedPreferenceChangeListener() {
    876   public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    877     // listener implementation
    878   }
    879 });
    880 </pre>
    881 
    882 <p>  
    883 </p>
    884 
    885 <pre>
    886 SharedPreferences.OnSharedPreferenceChangeListener listener =
    887     new SharedPreferences.OnSharedPreferenceChangeListener() {
    888   public void onSharedPreferenceChanged(SharedPreferences prefs, String key) {
    889     // listener implementation
    890   }
    891 };
    892 prefs.registerOnSharedPreferenceChangeListener(listener);
    893 </pre>
    894 
    895 <h2 id="NetworkUsage"></h2>
    896 
    897 
    898 <p>Android 4.0  
    899  
    900 
    901 
    902 <p>
    903 
    904 <p>Wi-Fi 
    905 
    906 
    907 
    908 </p>
    909 
    910 <p> {@link android.preference.PreferenceActivity}   {@link android.content.Intent#ACTION_MANAGE_NETWORK_USAGE}  
    911 
    912 </p>
    913 
    914 <pre>
    915 &lt;activity android:name="SettingsActivity" ... >
    916     &lt;intent-filter>
    917        &lt;action android:name="android.intent.action.MANAGE_NETWORK_USAGE" />
    918        &lt;category android:name="android.intent.category.DEFAULT" />
    919     &lt;/intent-filter>
    920 &lt;/activity>
    921 </pre>
    922 
    923 <p> 
    924 [<em>View application settings</em>] {@link android.preference.PreferenceActivity} 
    925 
    926 
    927 </p>
    928 
    929 
    930 
    931 
    932 
    933 
    934 
    935 <h2 id="Custom"> </h2>
    936 
    937 <p>Android  UI  {@link android.preference.Preference} 
    938 
    939 
    940 {@link android.preference.Preference}  1  
    941 </p>
    942 
    943 <p>{@link android.preference.Preference} 
    944 </p>
    945 
    946 <ul>
    947   <li> </li>
    948   <li></li>
    949   <li>{@link android.preference.Preference} 
    950 </li>
    951   <li></li>
    952   <li>{@link android.preference.Preference}  UI
    953 </li>
    954 </ul>
    955 
    956 <p></p>
    957 
    958 
    959 
    960 <h3 id="CustomSelected"> </h3>
    961 
    962   <p>{@link android.preference.Preference} {@link android.preference.Preference#onClick()} 
    963 
    964  {@link android.preference.DialogPreference} 
    965 {@link android.preference.DialogPreference}   {@link android.preference.DialogPreference#setDialogLayoutResource setDialogLayoutResourcs()} 
    966 
    967 
    968 </p>
    969 
    970   <p> {@link android.preference.DialogPreference}  
    971 
    972 </p>
    973 
    974 <pre>
    975 public class NumberPickerPreference extends DialogPreference {
    976     public NumberPickerPreference(Context context, AttributeSet attrs) {
    977         super(context, attrs);
    978 
    979         setDialogLayoutResource(R.layout.numberpicker_dialog);
    980         setPositiveButtonText(android.R.string.ok);
    981         setNegativeButtonText(android.R.string.cancel);
    982 
    983         setDialogIcon(null);
    984     }
    985     ...
    986 }
    987 </pre>
    988 
    989 
    990 
    991 <h3 id="CustomSave"></h3>
    992 
    993 <p>{@link android.preference.Preference#persistBoolean persistBoolean()} {@link android.preference.Preference#persistInt persistInt()} {@link android.preference.Preference}  {@code persist*()} 
    994 
    995 
    996 </p>
    997 
    998 <p class="note"><strong>:</strong>  {@link android.preference.Preference} 1  {@link android.preference.Preference}  {@code persist*()} 
    999 
   1000 </p>
   1001 
   1002 <p> {@link android.preference.Preference} 
   1003 {@link android.preference.DialogPreference}  [OK] 
   1004 
   1005 </p>
   1006 
   1007 <p>{@link android.preference.DialogPreference}  {@link android.preference.DialogPreference#onDialogClosed onDialogClosed()} 
   1008 Positive &mdash;  <code>true</code>  
   1009 
   1010 
   1011 </p>
   1012 
   1013 <pre>
   1014 &#64;Override
   1015 protected void onDialogClosed(boolean positiveResult) {
   1016     // When the user selects "OK", persist the new value
   1017     if (positiveResult) {
   1018         persistInt(mNewValue);
   1019     }
   1020 }
   1021 </pre>
   1022 
   1023 <p><code>mNewValue</code>  1 
   1024 {@link android.preference.Preference#persistInt persistInt()}  {@link android.content.SharedPreferences}  {@link android.preference.Preference}  XML 
   1025 
   1026 </p>
   1027 
   1028 
   1029 <h3 id="CustomInitialize"></h3>
   1030 
   1031 <p> {@link android.preference.Preference} {@link android.preference.Preference#onSetInitialValue onSetInitialValue()} 
   1032 
   1033 
   1034 </p>
   1035 
   1036 <p> {@link android.preference.Preference#onSetInitialValue onSetInitialValue()}  <code>restorePersistedValue</code> 
   1037 
   1038 <code>true</code> {@link android.preference.Preference}  {@code getPersisted*()}  {@link android.preference.Preference#getPersistedInt getPersistedInt()} 
   1039 
   1040 
   1041 UI 
   1042 
   1043 </p>
   1044 
   1045 <p><code>restorePersistedValue</code>  <code>false</code> 2 
   1046 </p>
   1047 
   1048 <pre>
   1049 &#64;Override
   1050 protected void onSetInitialValue(boolean restorePersistedValue, Object defaultValue) {
   1051     if (restorePersistedValue) {
   1052         // Restore existing state
   1053         mCurrentValue = this.getPersistedInt(DEFAULT_VALUE);
   1054     } else {
   1055         // Set default state from the XML attribute
   1056         mCurrentValue = (Integer) defaultValue;
   1057         persistInt(mCurrentValue);
   1058     }
   1059 }
   1060 </pre>
   1061 
   1062 <p> {@code getPersisted*()} 
   1063 {@link android.preference.Preference#getPersistedInt getPersistedInt()} 
   1064 
   1065 </p>
   1066 
   1067 <p class="caution"><strong>:</strong> <code>defaultValue</code> <code>restorePersistedValue</code>  <code>true</code>  null {@code getPersisted*()} <strong></strong>
   1068 
   1069 </p>
   1070 
   1071 
   1072 <h3 id="CustomDefault"></h3>
   1073 
   1074 <p>{@link android.preference.Preference} {@code android:defaultValue}  {@link android.preference.Preference#onGetDefaultValue onGetDefaultValue()} 
   1075 
   1076 
   1077  {@link android.content.SharedPreferences} 
   1078 
   1079 </p>
   1080 
   1081 <pre>
   1082 &#64;Override
   1083 protected Object onGetDefaultValue(TypedArray a, int index) {
   1084     return a.getInteger(index, DEFAULT_VALUE);
   1085 }
   1086 </pre>
   1087 
   1088 <p> {@code android:defaultValue} 
   1089  
   1090 
   1091 </p>
   1092 
   1093 
   1094 
   1095 <h3 id="CustomSaveState"></h3>
   1096 
   1097 <p> {@link android.view.View} {@link android.preference.Preference} 
   1098 
   1099 {@link android.preference.Preference}    {@link android.preference.Preference#onSaveInstanceState onSaveInstanceState()}  {@link android.preference.Preference#onRestoreInstanceState onRestoreInstanceState()} 
   1100 
   1101 
   1102 
   1103 </p>
   1104 
   1105 <p>{@link android.preference.Preference} {@link android.os.Parcelable} 
   1106 Android {@link android.preference.Preference.BaseSavedState} 
   1107 
   1108 </p>
   1109 
   1110 <p>{@link android.preference.Preference} {@link android.preference.Preference.BaseSavedState} 
   1111  {@link android.preference.Preference.BaseSavedState#CREATOR} 
   1112 
   1113 </p>
   1114 
   1115 <p>{@link android.preference.Preference} {@code value} 
   1116 
   1117 </p>
   1118 
   1119 <pre>
   1120 private static class SavedState extends BaseSavedState {
   1121     // Member that holds the setting's value
   1122     // Change this data type to match the type saved by your Preference
   1123     int value;
   1124 
   1125     public SavedState(Parcelable superState) {
   1126         super(superState);
   1127     }
   1128 
   1129     public SavedState(Parcel source) {
   1130         super(source);
   1131         // Get the current preference's value
   1132         value = source.readInt();  // Change this to read the appropriate data type
   1133     }
   1134 
   1135     &#64;Override
   1136     public void writeToParcel(Parcel dest, int flags) {
   1137         super.writeToParcel(dest, flags);
   1138         // Write the preference's value
   1139         dest.writeInt(value);  // Change this to write the appropriate data type
   1140     }
   1141 
   1142     // Standard creator object using an instance of this class
   1143     public static final Parcelable.Creator&lt;SavedState> CREATOR =
   1144             new Parcelable.Creator&lt;SavedState>() {
   1145 
   1146         public SavedState createFromParcel(Parcel in) {
   1147             return new SavedState(in);
   1148         }
   1149 
   1150         public SavedState[] newArray(int size) {
   1151             return new SavedState[size];
   1152         }
   1153     };
   1154 }
   1155 </pre>
   1156 
   1157 <p>{@link android.preference.Preference.BaseSavedState} {@link android.preference.Preference} {@link android.preference.Preference}  {@link android.preference.Preference#onSaveInstanceState onSaveInstanceState()}  {@link android.preference.Preference#onRestoreInstanceState onRestoreInstanceState()} 
   1158 
   1159 
   1160 
   1161 
   1162 </p>
   1163 
   1164 <p></p>
   1165 
   1166 <pre>
   1167 &#64;Override
   1168 protected Parcelable onSaveInstanceState() {
   1169     final Parcelable superState = super.onSaveInstanceState();
   1170     // Check whether this Preference is persistent (continually saved)
   1171     if (isPersistent()) {
   1172         // No need to save instance state since it's persistent,
   1173         // use superclass state
   1174         return superState;
   1175     }
   1176 
   1177     // Create instance of custom BaseSavedState
   1178     final SavedState myState = new SavedState(superState);
   1179     // Set the state's value with the class member that holds current
   1180     // setting value
   1181     myState.value = mNewValue;
   1182     return myState;
   1183 }
   1184 
   1185 &#64;Override
   1186 protected void onRestoreInstanceState(Parcelable state) {
   1187     // Check whether we saved the state in onSaveInstanceState
   1188     if (state == null || !state.getClass().equals(SavedState.class)) {
   1189         // Didn't save the state, so call superclass
   1190         super.onRestoreInstanceState(state);
   1191         return;
   1192     }
   1193 
   1194     // Cast state to custom BaseSavedState and pass to superclass
   1195     SavedState myState = (SavedState) state;
   1196     super.onRestoreInstanceState(myState.getSuperState());
   1197 
   1198     // Set this Preference's widget to reflect the restored state
   1199     mNumberPicker.setValue(myState.value);
   1200 }
   1201 </pre>
   1202 
   1203