UnifiedPreference Documentation

repository·master·Indexed 19 days ago

https://github.com/saik0/unifiedpreference

A lightweight Android library providing a consistent preference interface across API versions v4 and up. It simplifies the implementation of complex preference layouts, including automatic dual-pane views for tablets based on system properties. Key components include UnifiedPreferenceActivity and UnifiedPreferenceFragment.

Tokens
699
Snippets
2
Records
3
Agent score
18%

What's inside UnifiedPreference

  1. How dual-pane layout is determined

    master

    UnifiedPreference automatically decides whether to use a single-pane layout (for phones) or a two-pane layout (headers and fragments for tablets) based on the system property com.android.internal.R.bool.preferences_prefer_dual_pane.

    In AOSP, this typically evaluates to true for sw720dp configurations, but behavior may vary depending on the device OEM or third-party ROM.

  2. Implement UnifiedPreferenceActivity

    master

    To use the library, subclass UnifiedPreferenceActivity (or UnifiedSherlockPreferenceActivity if using ActionBarSherlock/ABS).

    In your Activity's onCreate method, you must call setHeaderRes(R.xml.your_header_file) before calling super.onCreate(savedInstanceState).

    Your fragments should subclass UnifiedPreferenceFragment and are typically defined as static inner classes within your Activity.

    public class SampleActivity extends UnifiedPreferenceActivity {
    
        @Override 
        public void onCreate(Bundle savedInstanceState) {
            setHeaderRes(R.xml.pref_headers);
            super.onCreate(savedInstanceState);
        }
    
        public static class SampleFragment extends UnifiedPreferenceFragment {}
    }
  3. Configure preference headers XML

    master

    To implement a multi-pane layout (headers and fragments), you must create a header XML file using your project's own namespace for the unified: attributes. Each <header> element must define a unified:preferenceRes which points to a standard Android preference XML file containing the actual settings for that section.

    Required attributes for <header>:

    • unified:fragment: The fully qualified class name of the fragment to use (e.g., your.package.Name$FragmentName).
    • unified:title: A string resource for the header title.
    • unified:preferenceRes: The XML resource ID of the preference file for this header.
    <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:unified="http://schemas.android.com/apk/res-auto" >
    
        <header
            unified:fragment="your.project.namespace.SampleActivity$SampleFragment"
            unified:title="@string/pref_header_sample"
            unified:preferenceRes="@xml/pref_sample" />
    
    </preference-headers>