Your settings activity theme needs to specify the following values:
<stylename="AppTheme"parent="Theme.AppCompat.Light.NoActionBar">
<!-- Used to theme preference list and items. -->
<itemname="preferenceTheme">@style/PreferenceThemeOverlay.Asp.Material</item>
<!-- Default icon tint for preferences. -->
<itemname="asp_preferenceIconTint">?colorAccent</item>
<itemname="asp_preferenceDialogIconTint">?asp_preferenceIconTint</item>
</style>
Recommended tint colors are ?colorAccent or ?colorControlNormal.
Styling alertDialogTheme is recommended for a proper color theme. See the sample project.
Dividers
AndroidX Preference provides a divider implementation out of the box.
If you want to customize how this divider looks you can call setDivider(...) and setDividerHeight(...).
The divider will be drawn just between items and at the bottom of the list. It will not be drawn before the end of category.
If you want more control over where the dividers are drawn, disable the default implementation
and enable the one from this library:
RingtonePicker will show only system ringtones/notification sounds by default.
If you want to include sounds from the external storage your app needs to request
android.permission.READ_EXTERNAL_STORAGE permission in its manifest.
Don't forget to check this runtime permission before opening ringtone picker on API 23.
ListPreference custom adapter
If you subclass ListPreference you can supply your own SpinnerAdapter which may or may not
use getEntries() as its data set. Here follow the methods you need to override:
SpinnerAdapter buildSimpleMenuAdapter(Context) - Used in simple menus.
SpinnerAdapter buildSimpleDialogAdapter(Context) - Used in simple dialogs.
Override the following methods if your SpinnerAdapter does not use getEntries() as data set:
Theme overlay applied via android:theme directly limits its effects on this widget.
Is not supported by AppCompat or platform popup windows.
Spinner will have proper space around its caret before API 23.
If using style="@style/Widget.Material.Spinner" or style="@style/Widget.Material.Spinner.Underlined".
If you need to alter entries programmatically create by CheckedItemAdapter.newInstance(Context, CharSequence[], int)
or supply your own adapter (responsible for its own styling) to XpAppCompatSpinner.setAdapter(SpinnerAdapter).
Attribute
Values
Description
app:asp_spinnerMode
dropdown
Menu is shown in a popup window. Selected option is highlighted. Less disruptive.
dialog
Menu is shown in a dialog with no other controls. Selected option is highlighted.
adaptive
Default behavior. Menu is shown in a popup window if it contains only single line items. Otherwise simple dialog is shown.
app:asp_simpleMenuWidthUnit
0dp
Default behavior. Popup window width is determined by the width of its content.
*X*dp
Popup width is determined by the width of its content and rounded up to the nearest multiply of Xdp.
app:asp_simpleMenuWidthMode
match_constraint
Popup width will stretch to match underlying anchor width.
wrap_content
Default behavior. Popup window width is determined by the width of its content.
wrap_content_unit
Popup width is determined by the width of its content and rounded up to the nearest multiply of Xdp.
app:asp_simpleMenuMaxWidth
fit_anchor
Popup width can stretch up to width of the underlying anchor.
fit_screen
Default behavior. Popup width can stretch up to width of the screen.
app:asp_simpleMenuMaxItemCount
-1
Default behavior. Popup will show as many items as fit on the screen.
Specifies the number of columns in the color picker. Use an integer resource which will allow you to specify greater number on tablets. Default is 4.
-1
Number of columns will be computed automatically to fill space available in window.
app:asp_swatchSize
small
Default option. Swatches will be 48dp in width and height plus 4dp margin on each side.
large
Swatches will be 64dp in width and height plus 8dp margin on each side.
Finally you need to make your preference fragment fire up the color picker dialog
when the preference is clicked and optionally update summary when a color is chosen.
Please review sample SettingsActivity.java
and SettingsFragment.java respectively.
If you need to change the default style either use style attribute or override it in your theme:
The color is stored internally as a 32-bit integer.
Subscreen navigation
Possible solution is implemented in PreferenceScreenNavigationStrategy.ReplaceFragment class.
This class will help you replace the whole preference fragment with a new instance with specified root preference.
It is using fragment transactions and back stack allowing for transition animations and saved states.
This class also supports Up button navigation which synthesizes preference screen stack on-the-fly.
Please see the sample project for details.
Known issues with support library
You may have experienced unexpected background color which manifests as holo blue on Android 4 and grey on Android 5.
This is caused by PreferenceFragment's RecyclerView grabbing focus on fragment start.
We can disable this behavior while still being able to navigate between individual preferences with a D-pad.
@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final RecyclerView listView = getListView();
// We don't want this. The children are still focusable.
listView.setFocusable(false);
}
Icon tinting
All preferences:
app:asp_tint
app:asp_tintMode
app:asp_tintEnabled
All dialog preferences:
app:asp_dialogTint
app:asp_dialogTintMode
app:asp_dialogTintEnabled
Icon padding
Application icons (48dp x 48dp) require no extra padding.
For smaller icons extra padding of 4dp on each side is needed.
Achieve this by using app:asp_iconPaddingEnabled
and app:asp_dialogIconPaddingEnabled attributes. Icon padding is enabled by default.
Handling PreferenceScreen icons
As PreferenceScreen class is final and hardwired into preference system
I was unable to automate icon tinting and padding. However you are able to do this yourself:
Preferencesubs = findPreference("subscreen");
PreferenceIconHelpersubsHelper = newPreferenceIconHelper(subs);
subsHelper.setIconPaddingEnabled(true); // Call this BEFORE setIcon!subsHelper.setIcon(R.drawable.some_icon);
subsHelper.setTintList(AppCompatResources.getColorStateList(subs.getContext(), R.color.accent));
subsHelper.setIconTintEnabled(true);
/* or */PreferenceIconHelper.setup(subs/* preference */,
R.drawable.some_icon/* icon */,
R.color.accent/* tint */,
true/* padding */);
You can use this class even on preference classes from androidx.preference package
in case you're not using XpPreferenceFragment.
Here's an example how to get error reports to Crashlytics:
class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
// Setup Crashlytics, do whatever else you need...
XpSupportPreferencePlugins.registerErrorInterceptor(new ErrorInterceptor() {
@Override
public void onError(@NonNull Throwable t, @Nullable String message) {
Timber.w(t, message);
if (message != null) Crashlytics.log(message);
Crashlytics.logException(t);
}
});
}
}
Only register the listener once per app process!
When reporting your findings here make sure to strip any user or sensitive data.
What's needed:
Android version
Device manufacturer and model
Stacktrace with line numbers preserved
Android support library version
xpece-android-support-preference library version
Thanks for making this library better!
Credit
Most of this library is straight up pillaged latest SDK mixed with heavy reliance
on AndroidX AppCompat and Preference. Kudos to people who create and maintain these!
请发表评论