martin-stone / hsv-alpha-color-picker-android

A color picker and a color preference for use in Android applications.
Apache License 2.0
290 stars 60 forks source link

PreferenceManager.setDefaultValues not picking default values #26

Closed vauywaves closed 7 years ago

vauywaves commented 8 years ago

Hi, this is a great library for Android Color picker with alpha and hex display options. Tons of thanks for such a wonderful library.

While using it in one of my apps, in settings.xml i provide a value for app:colorpicker_defaultColor. Also in my Launcher Activity i am initializing preferences values from their defaults as PreferenceManager.setDefaultValues(this, R.xml.settings, false);

It seems PreferenceManager is not setting the default values for com.rarepebble.colorpicker.ColorPreference. Kindly confirm, am i missing something.

Thanks & Regards

martin-stone commented 8 years ago

There are a couple of issues here:

  1. The colorpicker gets it's default from app:colorpicker_defaultColor rather than android:defaultValue.
  2. setDefaultValues() may not do what you think it does. The docs say: "Note: this will NOT reset preferences back to their default values".

(1) is a hack that I'm looking at fixing. Regarding (2), you may need something like the following to reset your colors. (Works for me):

getPreferenceManager().getDefaultSharedPreferences(getActivity()).edit().clear().commit();
getActivity().recreate(); // Needed to refresh the views :-(
martin-stone commented 8 years ago

I've just released version 2.0, in which I've got android:defaultValue working. After upgrading, you'll need to use that instead of any app:colorpicker_defaultColor settings you have.

This might help you with what you're trying to achieve, although the code above works for me in any case.

Does any of the above solve your problem?

martin-stone commented 7 years ago

I'll interpret silence as "problem solved".