A convinience library for working with all versions of the Android Preference package from API v4 and up

Overview

UnifiedPreference

UnifiedPreference is a library for working with all versions of the Android Preference package from API v4 and up.

Features

  • Easy to use
  • Takes care of most of the boilerplate code
  • Tiny, only ~5k!
  • Use a single pane for phones, or headers and fragments for tablets
  • Binds preference values to summaries according to Android Design Guidelines
  • Translated to 50 locales (imported from AOSP internal strings)
  • Uses ActionBarSherlock (optional)

How to Use

  1. Ensure your project has one preference xml file for each desired header

  2. Create or modify a header xml file using your project's own namespace for header attributes. Be sure to define preferenceRes for each.

    <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>
  3. Subclass UnifiedPreferenceActivity (or UnifiedSherlockPreferenceActivity for ABS). Call setHeaderRes before super.onCreate and subclass the fragments here.

    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 {}

Thats it! See the demo project for a more in depth example of how to use the library.

Author

Screenshots

Framed artwork

FAQ

When is a two-pane layout used?

When com.android.internal.R.bool.preferences_prefer_dual_pane evaluates to true. In AOSP this is true for sw720dp but this may vary by OEM or third party ROM.

License

Copyright 2012 Joel Pedraza

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

   http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Comments
  • Fragment code is completely ignored

    Fragment code is completely ignored

    I have a very simple setup following your demo project

    <preference-headers xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:unified="http://schemas.android.com/apk/res/bostone.android.jobrio" >
        <header
            unified:fragment="bostone.android.jobrio.JobrioPreferenceActivity$AccountsFragment"
            unified:title="@string/pref_header_general"
            unified:preferenceRes="@xml/pref_general" />
    </preference-headers>
    

    pref_general.xml

    <PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
        <CheckBoxPreference
            android:defaultValue="false"
            android:key="linkedin_login"
            android:summary="@string/pref_description_linkedin_account"
            android:title="@string/pref_title_linkedin_account" />
    </PreferenceScreen>
    

    And the java code

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            setHeaderRes(R.xml.pref_headers);
            setSharedPreferencesName(getApplicationContext().getPackageName());
            setSharedPreferencesMode(Context.MODE_PRIVATE);
            super.onCreate(savedInstanceState);
            final ActionBar ab = getSupportActionBar();
            ab.setDisplayHomeAsUpEnabled(true);
        }
    
        public static class AccountsFragment extends UnifiedPreferenceFragment {
    
            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }
    
            @Override
            public void onAttach(Activity activity) {
                super.onAttach(activity);
            }
    
        }
    

    Preferences screen comes up just fine and checkbox property acts appropriately. What puzzle me is that unified:fragment="bostone.android.jobrio.JobrioPreferenceActivity$AccountsFragment" is seemingly ignored and in fact I can put any gibberish as fragment value and it will not generate any errors. As far as AccountsFragment goes - its onCreate and onAttach methods are never called.

    I must be doing something wrong here?

    Thanks,

    Bo Stone

    question 
    opened by bostone 11
  • Custom Pref Buttons are not working

    Custom Pref Buttons are not working

    Hi today I tried to integrate your lib. It supposed to be easy but it is not. Here is the problem.

    My pref_headers.xml

    <header
        unified:fragment="com.tasomaniac.hdfilmizle.preference.GeneralPreferenceFragment"
        unified:title="@string/settings"
        unified:preferenceRes="@xml/pref_general" />
    <header
        unified:fragment="com.tasomaniac.hdfilmizle.preference.OnbellekPreferenceFragment"
        unified:title="Önbellek Ayarları"
        unified:preferenceRes="@xml/pref_onbellek" />
    

    pref_onbellek.xml is here:

        <CheckBoxPreference
            android:summary="@string/pref_summary_cache"
            android:title="@string/pref_title_cache_flag"
            android:key="@string/pref_key_flag_cache"
            android:defaultValue="true" />
         <Preference
            android:title="@string/pref_title_erasecache_flag"
            android:key="erasecache_flag" />
    

    Here I have item. In OnbellekPreferenceFragment.java I am calling it like below

    @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

            Preference customPref = (Preference) findPreference("erasecache_flag");
            customPref.setOnPreferenceClickListener(new OnPreferenceClickListener() {
    
                public boolean onPreferenceClick(Preference preference) {
                    Toast.makeText(getActivity().getApplicationContext(),"Önbellek Silindi!",Toast.LENGTH_LONG).show();
    
    
                        new Thread(new Runnable() {
                            public void run() {
                                new ImageLoader(getActivity().getApplicationContext()).clearCache();
                            }
                        }).start();
                        return true;
    
                }
            });
        }
    

    The problem is that it is not working. Actually I had a break point before super.onCreate() function. The breakpoint was not called at all. What can be a problem? When I supply an intent it is working.

    question 
    opened by tasomaniac 8
  • Crash in tablet

    Crash in tablet

    Hey, I'm using the unified preference and it works perfectly fine in two of my devices (smasung galaxy s2 and nexus 4) but when i try it on my galaxy tab it has an exception in the line: super.onCreate(savedInstanceState);

    the exception log: 08-03 22:06:08.619: E/AndroidRuntime(16620): FATAL EXCEPTION: main 08-03 22:06:08.619: E/AndroidRuntime(16620): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.out.goout/com.out.goout.SettingsActivity}: android.app.Fragment$InstantiationException: Unable to instantiate fragment net.saik0.android.unifiedpreference.demo.DemoUnifiedPreferenceActivity$GeneralPreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread.access$600(ActivityThread.java:140) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.os.Handler.dispatchMessage(Handler.java:99) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.os.Looper.loop(Looper.java:137) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread.main(ActivityThread.java:4895) 08-03 22:06:08.619: E/AndroidRuntime(16620): at java.lang.reflect.Method.invokeNative(Native Method) 08-03 22:06:08.619: E/AndroidRuntime(16620): at java.lang.reflect.Method.invoke(Method.java:511) 08-03 22:06:08.619: E/AndroidRuntime(16620): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994) 08-03 22:06:08.619: E/AndroidRuntime(16620): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761) 08-03 22:06:08.619: E/AndroidRuntime(16620): at dalvik.system.NativeStart.main(Native Method) 08-03 22:06:08.619: E/AndroidRuntime(16620): Caused by: android.app.Fragment$InstantiationException: Unable to instantiate fragment net.saik0.android.unifiedpreference.demo.DemoUnifiedPreferenceActivity$GeneralPreferenceFragment: make sure class name exists, is public, and has an empty constructor that is public 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.Fragment.instantiate(Fragment.java:584) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.preference.PreferenceActivity.switchToHeaderInner(PreferenceActivity.java:1230) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.preference.PreferenceActivity.switchToHeader(PreferenceActivity.java:1263) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.preference.PreferenceActivity.onCreate(PreferenceActivity.java:630) 08-03 22:06:08.619: E/AndroidRuntime(16620): at com.out.goout.SettingsActivity.onCreate(SettingsActivity.java:19) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.Activity.performCreate(Activity.java:5163) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061) 08-03 22:06:08.619: E/AndroidRuntime(16620): ... 11 more 08-03 22:06:08.619: E/AndroidRuntime(16620): Caused by: java.lang.ClassNotFoundException: net.saik0.android.unifiedpreference.demo.DemoUnifiedPreferenceActivity$GeneralPreferenceFragment 08-03 22:06:08.619: E/AndroidRuntime(16620): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 08-03 22:06:08.619: E/AndroidRuntime(16620): at java.lang.ClassLoader.loadClass(ClassLoader.java:501) 08-03 22:06:08.619: E/AndroidRuntime(16620): at java.lang.ClassLoader.loadClass(ClassLoader.java:461) 08-03 22:06:08.619: E/AndroidRuntime(16620): at android.app.Fragment.instantiate(Fragment.java:574) 08-03 22:06:08.619: E/AndroidRuntime(16620): ... 18 more

    my code is:

    import com.actionbarsherlock.view.MenuItem;

    import db.GlobalSettings;

    import net.saik0.android.unifiedpreference.UnifiedSherlockPreferenceActivity; import android.content.Context; import android.os.Bundle;

    public class SettingsActivity extends UnifiedSherlockPreferenceActivity {
    @Override protected void onCreate(Bundle savedInstanceState) { setHeaderRes(R.xml.pref_headers); setSharedPreferencesName("com.out.goout_preferences"); setSharedPreferencesMode(Context.MODE_PRIVATE); super.onCreate(savedInstanceState);

        try 
        {
            getSherlock().getActionBar().setTitle(R.string.title_activity_settings);
            getSherlock().getActionBar().setHomeButtonEnabled(true);
            getSherlock().getActionBar().setDisplayHomeAsUpEnabled(true);   
        }
        catch (Exception e) {}
    }
    
    @Override
    public boolean onOptionsItemSelected(MenuItem item) 
    {
        if(item.getItemId() == android.R.id.home)
        {
            handleFinish();
            finish();
        }
        return super.onOptionsItemSelected(item);
    }
    
    /**this method get the name and the */
    private void handleFinish()
    {
        try 
        {
            if(!GlobalSettings.getSettings(getApplicationContext()).isUserDetSent())
            {
                String age = GlobalSettings.getSettings(getApplicationContext()).getUserAge();
                String gender = GlobalSettings.getSettings(getApplicationContext()).getUserGender();
    
                if(!age.equals("") && !gender.equals(""))
                {
                    //send the details to the server
                    GlobalSettings.getSettings(getApplicationContext()).setUserDetSent(true);
                }
            }
        }
        catch (Exception e){}
    }
    
    @Override
    public void onBackPressed() 
    {
        handleFinish();
        super.onBackPressed();
    }
    

    }

    your help is much needed. Thanks in advance.

    Best, Shalom Melamed

    opened by shalommelamed 2
  • Library don't compile with Eclipse ADT

    Library don't compile with Eclipse ADT

    I get this error, if I try to compile the library:

    [2012-11-08 19:22:39 - library] ERROR: In <declare-styleable> PreferenceHeader, unable to find attribute icon
    [2012-11-08 19:22:39 - library] ERROR: In <declare-styleable> PreferenceHeader, unable to find attribute title
    
    question 
    opened by Leandros 2
  • pref_headers cannot be resolved or is not a field

    pref_headers cannot be resolved or is not a field

    Adding to the project library created a folder xml for a file called hml. In the activity of adding the file and get an error

    package ru.test.android.enviroment;
    
    import android.R;
    import android.content.Context;
    import android.os.Bundle;
    
    import net.saik0.android.unifiedpreference.UnifiedPreferenceFragment;
    import net.saik0.android.unifiedpreference.UnifiedSherlockPreferenceActivity;
    
    public class PreferenceActivity extends UnifiedSherlockPreferenceActivity {
    
        @Override public void onCreate(Bundle savedInstanceState) {
            // Set header resource MUST BE CALLED BEFORE super.onCreate
                    setHeaderRes(R.xml.pref_headers);
                    // Set desired preference file and mode (optional)
        }
    
        public static class GeneralPreferenceFragment extends UnifiedPreferenceFragment {}
    }
    

    Error: pref_headers cannot be resolved or is not a field PreferenceActivity.java /SplashActivity/src/ru/test/android/enviroment line 14 Java Problem

    opened by Ibochkarev 1
  • perferenceheader bug

    perferenceheader bug

    every attr has to have a format field in preferenceheader in values files..

    otherwise you get aapt erros..its hanging on title and icon..so i am putting string format for title and integer format for icon.

    opened by fredgrott 1
  • Errors after importing project to Eclipse

    Errors after importing project to Eclipse

    [2013-03-06 15:39:16 - MeinProspekt] Error in an XML file: aborting build. [2013-03-06 16:17:35 - library] ERROR: In PreferenceHeader, unable to find attribute icon [2013-03-06 16:17:35 - library] ERROR: In PreferenceHeader, unable to find attribute title [2013-03-06 16:18:25 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute icon [2013-03-06 16:18:25 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute title [2013-03-06 16:18:48 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute icon [2013-03-06 16:18:48 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute title [2013-03-06 16:19:28 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute icon [2013-03-06 16:19:28 - UnifiedPreferenceLibrary] ERROR: In PreferenceHeader, unable to find attribute title

    opened by Malachiasz 1
  • No actionbar inhoneycomb?

    No actionbar inhoneycomb?

    Hi,

    Can't see the actionbar (and thus a menu option) on honeycomb. My app project has a library set to unifiedpreference which in turn has a library set to ActionBarSherlock. Works fine in Jelly Bean and Gingerbread but unfortunately not on Honeycomb (3.2) Any clues??

    opened by realjax 0
  • Doesn't work with Intellij!

    Doesn't work with Intellij!

    Hi,

    I have tried to compile your example project. I added all the dependency needed (comp + sherlock).

    The issue is relative to UnifiedPreference / library / res / values / atrs.xml Intellij has an issue with PreferenceHeader

    <declare-styleable name="PreferenceHeader">
    

    Intellij doesn't find PreferenceHeader class

    In my project I have created a view with some custom XML properties and I use atrs.xml for that purpose. The point is that in atrs.xml, I point the custom view class to make it work. After in the XML layout I point the class path too.

    I noticed that there is no PreferenceHeader class in the project. And I don't know what to do!

    Can you help me ?

    Regards

    opened by pommedeterresautee 3
Owner
Joel Pedraza
Joel Pedraza
Preference subclass that goes into your PreferenceFragment.

IntegrationPreference This library is especially for extension apps developers. Some applications like (Muzei, Series Guide) has extension capabilitie

Said Tahsin Dane 13 Oct 8, 2020
An Android library containing a simple TableView and an advanced SortableTableView providing a lot of customisation possibilities to fit all needs.

SortableTableView for Android An Android library providing a TableView and a SortableTableView. Minimum SDK-Version: 11 | Compile SDK-Version: 25 | La

Ingo Schwarz 1.1k Dec 5, 2022
A new canvas drawing library for Android. Aims to be the Fabric.js for Android. Supports text, images, and hand/stylus drawing input. The library has a website and API docs, check it out

FabricView - A new canvas drawing library for Android. The library was born as part of a project in SD Hacks (www.sdhacks.io) on October 3rd. It is cu

Antwan Gaggi 1k Dec 13, 2022
Pulseq is a service for monitoring activity from all your devices.

Pulseq is inspired by technically-functional/heartbeat, which is licensed under the ISC license. The main idea of pulseq is to provide statistics on y

d1snin-dev 5 Sep 8, 2022
A simple launcher which displays all the apps on a RecyclerView trying to KISS

A simple launcher which displays all the apps on a RecyclerView trying to KISS

Alex Allafi 1 Jun 17, 2022
A backport of the Android 4.2 GlowPadView that works on the SDK on API levels 4+

GlowPadBackport A backport of the Android 4.2 GlowPadView that works on the SDK on API levels 4+. It is heavily based upon Google's own internal imple

Frakbot 246 Dec 9, 2022
Provides 9-patch based drop shadow for view elements. Works on API level 9 or later.

Material Shadow 9-Patch This library provides 9-patch based drop shadow for view elements. Works on API level 14 or later. Target platforms API level

Haruki Hasegawa 481 Dec 19, 2022
Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. The library is based on the code of Mario Klingemann.

Android StackBlur Android StackBlur is a library that can perform a blurry effect on a Bitmap based on a gradient or radius, and return the result. Th

Enrique López Mañas 3.6k Dec 29, 2022
Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Simple and lightweight UI library for user new experience, combining floating bottom navigation and bottom sheet behaviour. Simple and beautiful.

Rizki Maulana 118 Dec 14, 2022
Android library providing bread crumbs to the support library fragments.

Hansel And Gretel Android library providing bread crumbs for compatibility fragments. Usage For a working implementation of this project see the sampl

Jake Wharton 163 Nov 25, 2022
Janishar Ali 2.1k Jan 1, 2023
The ShowcaseView library is designed to highlight and showcase specific parts of apps to the user with an attractive and flat overlay.

The ShowcaseView library is designed to highlight and showcase specific parts of apps to the user with an attractive and flat overlay.

Mohammad Reza Eram 484 Dec 26, 2022
This library provides a simple way to add a draggable sliding up panel (popularized by Google Music and Google Maps) to your Android application. Brought to you by Umano.

Note: we are not actively responding to issues right now. If you find a bug, please submit a PR. Android Sliding Up Panel This library provides a simp

Umano: News Read To You 9.4k Dec 31, 2022
An Android library supports badge notification like iOS in Samsung, LG, Sony and HTC launchers.

ShortcutBadger: The ShortcutBadger makes your Android App show the count of unread messages as a badge on your App shortcut! Supported launchers: Sony

Leo Lin 7.2k Dec 30, 2022
Android swipe-to-dismiss mini-library and sample code

Android Swipe-to-Dismiss Sample Code Sample code that shows how to make ListView or other views support the swipe-to-dismiss Android UI pattern. See t

Roman Nurik 1.3k Dec 29, 2022
⚡️A highly customizable, powerful and easy-to-use alerting library for Android.

Flashbar A highly customizable, powerful and easy-to-use alerting library for Android. Specs This library allows you to show messages or alerts in you

Aritra Roy 1.7k Dec 7, 2022
GreenDroid is a development library for the Android platform. It makes UI developments easier and consistent through your applications.

#GreenDroid Foreword : This project, initially initiated by me, Cyril Mottier, is not maintained anymore and can be considered as deprecated. As a con

Cyril Mottier 2.6k Jan 4, 2023
FogView is a android library that can show fog on any layout and the fog removes when user rubs it.

Fog View Android Library Min SDK 8 (Android 2.2–2.2.3 Froyo) Screnshots How to use If you want use this library, you can download project and import i

Chetan Kaushik 631 Dec 31, 2022
Simple and powerful library to emulate iOS's "3D Touch" preview functionality on Android.

Android 3D Touch - PeekView iOS uses 3D Touch as a way to "peek" into full content, such as emails, pictures, web searches, etc. While they have dedic

Luke Klinker 502 Dec 29, 2022