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

Addon: Preference Framework

Sergey Shatunov edited this page Dec 2, 2013 · 3 revisions

Since 1.5 Preference Framework was be splited from main library as addon.

Maven

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

Non-maven users

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

Id instead key

Also now all Preference classes have a id support instead of key. You can specify id in xml preference layout and get it's 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'm put custom preference styles in activity theme, but nothing happened!

For preference items/views using other theme, not activity theme. You should remap theme with 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);
  }
}

Also you can use reference on preference theme in main theme by preferenceTheme attribute.

Clone this wiki locally