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

Problem getting color #34

Closed tomsr closed 7 years ago

tomsr commented 7 years ago

Hi, I am doing something wrong - how can I get already selected color code from preference screen, in my fragment class? Could not find in examples.

martin-stone commented 7 years ago

You should be able to load the value from shared preferences as an integer (which is how android represents colours), e.g...

https://stackoverflow.com/a/17916954

Use the same key that you specified in your preference screen xml.

tomsr commented 7 years ago

Hi, sorry do not understand, here how it looks from my side. Preference screen, all good, can go in and pick color. After exit it is saved somewhere, I guess shared preferences. Because when i come back in app second time, newly picked color is there.

PreferenceScreen start xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"

<com.rarepebble.colorpicker.ColorPreference
    android:key="listColor"
    android:title="@string/listColor"
    android:defaultValue="#abcfb4"
    />

PreferenceScreen end

Get color: SharedPreferences sharedPreferences = getActivity().getSharedPreferences("listColor", 0); int listColor = sharedPreferences.getInt("listColor", 0);

and I never get color. Where is the problem?

martin-stone commented 7 years ago

You should probably use getDefaultSharedpreferences(). "listColor" is probably not the name of your preferences file.

tomsr commented 7 years ago

Thanks Martin, Your color library is perfect. Worked as You said: SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext()); int listColor = prefs.getInt("listColor", 0); Thankyou very much.