The preference or Setting etc, you know what I mean and what they can do in your Android App.
You can ignore this repository if you only want to read Google guides about preference programming, by clicking here.
A one sheet which almost all preference options, this design has been deprecated and replaced by the master/slave or headers/sheet pattern.
A master/slave or headers/sheet pattern, which means we list a summary headers on the main preference screen and user can do selection to any of them to operate on one sheet under header.
-
A preference layout for one sheet or headers/sheet
-
preferenc 8000 e.xml, actually you can use any to name.
A one sheet layout with PreferenceScreen
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <CheckBoxPreference android:key="preference_save_data" android:title="Do you want to save data" android:summary="Save your application data if you checked" android:defaultValue="true" /> </PreferenceScreen>
A one sheet layout with v7 PreferenceScreen
<android.support.v7.preference.PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" > <android.support.v7.preference.CheckBoxPreference android:key="preference_save_data" android:title="Do you want to save data" android:summary="Save your application data if you checked" android:defaultValue="true" /> </android.support.v7.preference.PreferenceScreen>
or A list of headers
<preference-headers xmlns:android="http://schemas.android.com/apk/res/android" > <header android:title="Data save" android:summary="Option of saving data of application." android:fragment="com.example.SaveDataFragment"> <extra android:name="index" android:value="0"/> <extra android:name="header" android:value="preferences_save_data"/> <extra android:name="title" android:value="Save data"/> </header> <header android:title="Delete cache" android:summary="Clean application cache." android:fragment="com.example.CleanCacheFragment"> <extra android:name="index" android:value="1"/> <extra android:name="header" android:value="preferences_clear_cache"/> <extra android:name="title" android:value="Clean cache"/> </header> </preference-headers>
-
Using attributes in xml are easy to understand, they are real easy just like what they are :) . One thing interesting is "android:fragment" which declares a fragment to display a Sheet when user select an option on master. I reiterate this in the late doc.
-
-
Showing layout
-
PreferenceActivity shows a hierarchy of preferences to the user. Prior to Android 3.0 this class only allowed the display of a single sheet of preference, a single PreferenceScreen; this functionality should now be found in the new PreferenceFragment class, which means the PreferenceActivity contains list of Headers and PreferenceFragment shows a hierarchy of preferences to the user. That is why you see "android:fragment"s above which can server for each Header to open detail of it.
-
PreferenceFragment shows a hierarchy of Preference objects as lists like PreferenceActivity above. This class can work with general activities like a normal Fragment in order to take over sheet jobs and more this class was added after Android 3.0 for the master/slave or headers/sheet. The PreferenceFragment will be committed into PreferenceActivity automatically when the user selected a Header on the header-list. The PreferenceActivity will generate an Intent to info PreferenceFragment what preferences should be used by calling addPreferencesFromXXX functions.
-
-
Standard or v7-support
As mentioned before there are two versions of preferences on Android, you can use platform native or some from v7-support, except PreferenceActivity (But you can create one AppCompatPreferenceActivity )almost all elements have v7,i.e android.support.v7.preference.PreferenceScreen and PreferenceScreen. What special are fragments like PreferenceFragment and PreferenceFragmentCompat which provide a little different features.
-
Shared
- All fragments can show a hierarchy of Preference objects as lists.
- All fragments can work with any activities.
- You can use support-elements without package-name in xml for PreferenceFragmentCompat, they would be converted to node with package-names automatically while building.
-
Diff
-
PreferenceFragment works with standard components like PreferenceScreen, CheckBoxPreference etc.
-
PreferenceFragment enables to work with PreferenceActivity to realize master/slave automatically. What you have to do is to define header-list and the "android:fragment"s that point different PreferenceFragments to show a sheet(PreferenceScreen)
-
PreferenceFragmentCompat works with components like android.support.v7.preference.PreferenceScreen, android.support.v7.preference.CheckBoxPreference etc. There's no way to make master/slave or headers/sheet with PreferenceActivity even if you use "app:fragment" instead "android:fragment". A crash will be fired if "android:fragment" points to a PreferenceFragmentCompat .
-
Use v7 PreferenceScreen and v7 Preference to let PreferenceFragmentCompat build a "header-list" and assign "app:fragment"s to point target PreferenceFragmentCompat as "sheet". You might create two different layouts for phone and tablet which is done by PreferenceActivity automatically when you want master/slave or headers/sheet .
-
-
ActionBar or Toolbar
-
Generally there's a default theme works with PreferenceActivity which can or can't provide ActionBar, it depends on what kind of app-theme you define. However thank for the new
Theme.AppCompat
, we can create an AppCompatPreferenceActivity class to use support-actionbar. -
Use
Theme.AppCompat
as theme on the AppCompatPreferenceActivity you can get support-actionbar, however it's difficult to use the Toolbar withTheme.AppCompat.NoActionBar
when you don't use some tricks. The problem is that you can not write standard codes to locate the Toolbar. -
Use both fragments to work with app-compat activities is fine and both
Theme.AppCompat
,Theme.AppCompat.NoActionBar
run compatible along with support-actionbar or Toolbar.
-
The MIT License (MIT)
Copyright (c) 2016 Chris Xinyue Zhao
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.