Closed james-s-w-clark closed 6 years ago
Your code example gives me
java.lang.NumberFormatException: For input string: "0x00ff00"
But if I put...
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putInt("simplePreference", 0xff00ffff).commit();
...into the onCreate() of the demo app, it works perfectly for me (setting the colour to opaque cyan).
If you need more help with this, I'll need to see more code.
This fixed things - thanks! So the preference just had to be initialised with both alpha and rgb? I got the impression from the example code on the readme that just rgb was needed - but this was my mistake. I thought the value from "ColorPickerView usage" (....picker.setColor(0xff12345);...) would apply to initialising preferences.
edit: added PR which updates readme with SharedPreferences initialization at onCreate().
When I try to load preferences in my app, I keep getting the error "java.lang.RuntimeException: Unable to start activity ComponentInfo{.......MyPreferencesActivity}: java.lang.ClassCastException: java.lang.String cannot be cast to java.lang.Integer at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)"
In my activityMain I default the sharedPreference for a key value (upon first launch of the app):
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext()); SharedPreferences.Editor editor = sharedPreferences.edit(); editor.putInt("go_circle_colour_pref_key",Integer.parseInt("0x00ff00"));
In my preferences.xml, I have:
<com.rarepebble.colorpicker.ColorPreference android:key="go_circle_colour_pref_key" android:title="@string/goColour" app:colorpicker_showAlpha="true" />
I have tried:
putting default colours in the preferences.xml, e.g.
<!--android:defaultValue="#f00"-->
editor.putInt("go_circle_colour_pref_key",Integer.parseInt("0x00ff00"));
editor.putInt("go_circle_colour_pref_key",0x00ff00);
editor.putInt("go_circle_colour_pref_key",Color.parseColor("#00ff00");
editor.putString("go_circle_colour_pref_key","#00ff00";
editor.putString("go_circle_colour_pref_key","#0f0";
editor.putString("go_circle_colour_pref_key","0x00ff00";
If I give the color-picker preference in xml a key that doesn't actually get used (initialised in activityMain), then the colour picker works OK. I seem to be initialising the key incorrectly.