Skip to content

Preferences Activity

blurkidi edited this page Dec 3, 2013 · 5 revisions

You will usually implement your Preferences screen by extending PreferenceActivity or PreferenceFragment. Then you will inflate the preferences by calling addPreferencesFromResource(). To customize your activity you just need to follow the steps described in the basic setup guide. Then, there are some additional points to take into account in order to fully apply the accent color. They are described in the following sections.

Dialog Preferences

When we add a preference that displays a dialog when clicked, the dialog will not be properly styled. To avoid this HoloAccent provides custom classes that extend the native ones to apply the correct accent color. The classes are the following:

  • EditTextPreference
  • ListPreference
  • MultiSelectListPreference

Usually you specify the preferences in xml. So let's define a preference screen with one single preference of type EditTextPreference:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
	<com.negusoft.holoaccent.preference.EditTextPreference
		android:capitalize="words"
		android:defaultValue="@string/pref_default_edittext"
		android:inputType="textCapWords"
		android:key="example_text"
		android:maxLines="1"
		android:selectAllOnFocus="true"
		android:singleLine="true"
		android:title="@string/pref_title_edittext" />
</PreferenceScreen>

Note that we specify the whole classpath. So we are replacing the native 'EditTextPreference' by our implementation: 'com.negusoft.holoaccent.preference.EditTextPreference'. You can do just the same with the rest of the preferences.

Custom DialogPreferences

If you are extending the native DialogPreference to define your own, you can just use the implementation provided by HoloAccent. Nothing special to be done here.

Switch Preference

Finally, there is the SwitchPreference available. Just like for the dialog preferences, we will specify the HoloAccent implementation by including the correct classpath:

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
	<com.negusoft.holoaccent.preference.SwitchPreference
		android:key="example_switch"
		android:title="@string/pref_title_switch"
		android:summaryOn="@string/pref_summary_on_switch"
		android:summaryOff="@string/pref_summary_off_switch"
		android:switchTextOn="@string/pref_text_on_switch"
		android:switchTextOff="@string/pref_text_off_switch" />
</PreferenceScreen>

Clone this wiki locally