Adding a "Reset" Button to Change Preference Back to Its Default
If I have changed a preference value, then I want it to be easy to set the value back to the default, and be able to see that the preference is different than the default. So, I added a "Reset" button to each preference that shows up only if the value is different than the default, as shown below.
"Speech Speed" Preference, showing "Reset" Button that Appears only if Value is Different than Default |
Adding this extra button to the standard preference classes is accomplished by overriding the onCreateView method of the Preference class, and then:
- grab a handle to the ViewGroup that contains the standard widget (which has id android.R.id.widget_frame)
- make sure that the layout has a horizontal orientation (the stock one from the API has vertical orientation)
- create the new "Reset" button and adding this to the ViewGroup at index 0, so that it appears to the left of the existing widget
Gradient in the Background of the Preference Screen
Several of the preferences in my first app involved setting a color, and a small button of the chosen color is included in the list of preferences in these cases. Because these colors can be arbitrary, there is the chance that the button is hard to discern, no matter what background color is chosen. This situation is partially addressed by including a general slight gradient in the preference activity background itself, as shown below:
Left-to-Right Gradient in Preferences to Help User Discern Color Buttons for all Colors |
public static void setGradientForPreferenceView(View v) {
int[] colorsForGradient = new int[2];
colorsForGradient[0]=Color.argb(0, 0,0,0);
colorsForGradient[1]=Color.argb(64, 255,255,255);
GradientDrawable d = new GradientDrawable(
GradientDrawable.Orientation.LEFT_RIGHT ,
colorsForGradient);
d.setGradientType(GradientDrawable.LINEAR_GRADIENT);
d.setShape(GradientDrawable.RECTANGLE);
d.setDither(true);
v.setBackgroundDrawable(d);
}
I noticed that this gradient can consistently look banded, so following the information detailed by Eric Burke here, the format of the window is set to PixelFormat.RGBA_8888 in the (overridden) onAttachedToWindow method of the PreferenceActivity.
Note that setting the format of the window is apparently not supported for Android prior to level 7 (2.1), so if you must support those versions as well, you'll of course need to implement one of the design patterns to gracefully handle multiple API levels.
Color Picker
In all cases where the user needs to pick a color, a modified version of the color picker in the Android API demo package is used.
As with the original Android demo color picker on which this one is based, the user changes the possible color to pick by sliding their finger on the outer circle. I find this to be a very satisfying and intuitive way to pick the color. For the color picker in the Android demo, the user picks the color by clicking the middle circle. This functionality was retained, but because it was thought that this action might not be obvious to the user, an extra "Set Color" button is included.
Another addition was the insertion of a white-to-black gradient on the color wheel, so that the sweep gradient uses these colors:
mColors = new int[] {
0xFF000000, 0xFFFFFFFF,
0xFFFF00FF, 0xFF0000FF, 0xFF00FFFF, 0xFF00FF00,
0xFFFFFF00, 0xFFFF0000,
0xFF000000
};
An additional modification was the line emanating from the circle to the corresponding location on the outer circle. This helps the user to see exactly where on the wheel the color is. And for good measure, a slight animation effect is added by having little pulsing "beads" be "emitted" from the wheel, draining towards the middle, whenever the user has their finger on the color wheel.
And finally, the same transparent-to-light-white gradient for the preference itself is used as a background for the color picker, so as to help the middle circle to be more discernible no matter what the (arbitrary) color chosen is.
So there ya go - some simple tweaks that definitely filled some gaps for me when I needed to implement user preferences. Hope you find them useful, if for nothing else than as a starting place for your own customizations.
No comments:
Post a Comment