Skip to content

Preferences Activity

blurkidi edited this page Feb 22, 2014 · 5 revisions

You will usually implement your Preferences screen by extending 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.

Using PreferencesActivity

First of all, you shouldn't do this. PreferenceActivity is deprecated so you should use PreferenceFragment in stead, and then add them to your AccentActivity:

public class PreferencesActivity extends AccentActivity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Display the fragment as the main content.
		if (savedInstanceState == null) {
        		getFragmentManager().beginTransaction()
                		.replace(android.R.id.content, new GeneralPreferenceFragment())
                		.commit();
		}
	}
	public static class GeneralPreferenceFragment extends PreferenceFragment {
		@Override
		public void onCreate(Bundle savedInstanceState) {
			super.onCreate(savedInstanceState);
			addPreferencesFromResource(R.xml.preferences);
		}
	}
}

If you insist in using PreferenceActivity, it is still possible. Have a look at the setup without extending AccentActivity section in the pro tips & tricks page.

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