Skip to content

Commit caf4eee

Browse files
committed
Fix for setting DefaultValue as hex string
I have noticed that when you set defaultValue you can only set it as an actual color integer. 1. If you set it as hex string it crashes, as it expects integer 2. If you set it as reference to resources color, it persists 0 into shared preferences Method onGetDefaultValue was modified to look for a string and if it is not null, to use built in method to parse hex string into color integer and return it as default. Color hex can only be used with 6 or 8 chars. If the string is null, it will look for an integer and use it.
1 parent 135d725 commit caf4eee

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

ColorPickerPreference/src/main/java/net/margaritov/preference/colorpicker/ColorPickerPreference.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,15 @@ public ColorPickerPreference(Context context, AttributeSet attrs, int defStyle)
6666

6767
@Override
6868
protected Object onGetDefaultValue(TypedArray a, int index) {
69-
return a.getColor(index, Color.BLACK);
69+
int colorInt;
70+
String mHexDefaultValue = a.getString(index);
71+
if (mHexDefaultValue != null) {
72+
colorInt = convertToColorInt(mHexDefaultValue);
73+
return colorInt;
74+
75+
} else {
76+
return a.getColor(index, Color.BLACK);
77+
}
7078
}
7179

7280
@Override

0 commit comments

Comments
 (0)