Skip to content
This repository has been archived by the owner on Jun 5, 2018. It is now read-only.

Addon: Preference Framework

t-bullock edited this page Feb 18, 2014 · 3 revisions

Since 1.5 the Preference Framework has been split from the main library into an addon.

Maven

<dependency>
  <groupId>org.holoeverywhere</groupId>
  <artifactId>addon-preferences</artifactId>
  <version>${holoeverywhere.version}</version>
  <type>apklib</type>
</dependency>

Non-maven users

Import the project in addons/preferences and add as a dependency/module to your application.

Id instead of key

Now all Preference classes support id's rather than keys. You can now specify an id for an element in the xml preference layout and get its values by id.

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
  <CheckBoxPreference
    android:id="@+id/my_checkbox"
    android:title="CheckBox Sample" />
</PreferenceScreen>
import org.holoeverywhere.preference.PreferenceFragment;
import org.holoeverywhere.preference.SharedPreferences;
import org.holoeverywhere.preference.SharedPreferences.OnSharedPreferenceChangeListener;
import org.holoeverywhere.widget.Toast;

public class MyFragment extends PreferenceFragment implements OnSharedPreferenceChangeListener {
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
  }

  @Override
  public void onSharedPreferenceChanged(SharedPreferences preferences, String key) {
    Toast.makeText(this, "Value: " + preferences.getBoolean(R.id.my_checkbox, false), Toast.LENGTH_SHORT).show();
  }
}

I put custom preference styles in the activity theme, but nothing happened!

For preference items/views using other themes (not the activity theme), you should remap the theme with the PreferenceInit.map method.

<style name="CustomPreferenceTheme" parent="Holo.PreferenceTheme">
  <item name="checkBoxPreferenceStyle">@style/CustomPreferenceCheckBox</item>
</style>
<style name="CustomPreferenceTheme.Light" parent="Holo.PreferenceTheme.Light">
  <item name="checkBoxPreferenceStyle">@style/CustomPreferenceCheckBox</item>
</style>

In your Application class:

import org.holoeverywhere.app.Application;

public class MyApplication extends Application {
  static {
    PreferenceInit.map(R.style.CustomPreferenceTheme, R.style.CustomPreferenceTheme_Light);
  }
}

You can also use references to the preference theme in your main theme using the preferenceTheme attribute.