4

In my application is settings_activity where people can change some colors (icons, text etc). I want to put colors in sharedpreferences, created class:

public class AppData {
    static SharedPreferences prefdata;
    static SharedPreferences.Editor editordata;

    static final String  FCOLOR_KEY = "#FFFFFF"; //first color
    static final String SCOLOR_KEY = "#FFFFFF"; //second color
    static final String TCOLOR_KEY = "#FFFFFF"; //text color
    static final String ICOLOR_KEY = "#FFFFFF"; //icon color
    static final Image BIMG_KEY = null; //bakcground image

}

What is best value type for colors (int, string or just colors)?

How can I change values from appdata by use settings_activity and how can I use it (colors) in xml files? Should I use colors.xml(how?)?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
littlewombat
  • 299
  • 2
  • 8
  • 22
  • Are you pre-defining the colors, and then the user picks one? Or are you allowing the user to define a color through input? If it's the first, then just store your pre-defined colors in your colors.xml file and call them by their resource id. Then in your shared prefs just store some sort of identifier which refers to your static values (could be anything). You could even just store the resource id itself if you wanted to. – NoChinDeluxe Mar 29 '16 at 18:35
  • It's the second, I havent got any pre-defining colors. Application has default colors but user can change any of them (I want to do it in sharedpreferences so if user change anything it wont be back to default when application will restart?). For example users can put hex string. – littlewombat Mar 29 '16 at 18:42

2 Answers2

1

If you are going to use the color enumeration, I would just use an int to store it. (See below for data type of ints)

http://developer.android.com/reference/android/graphics/Color.html

However, if you are going to use the hexadecimal value, then I would store it as a string. When you load your app, check the shared preferences and load the string and if the option does not exist load a default color.

object.setColor(sharedPreferences.getString("COLOR", "#FFFFFF"));.
Alexander N.
  • 1,458
  • 14
  • 25
  • I'm sorry if it is totally basic question but I'm not sure where should I put this color (understand that get COLOR from sharedpreferences and if it is null put default (#FFFFFF). What is 'object'? How can I use this color in xml files? – littlewombat Mar 29 '16 at 19:02
  • You wouldn't use it in an XML file. Instead, what I would suggest you do is put this in your code for the object that requires that color and then use that objects set color method. Most objects (views) that have a color that you can select would allow you to use setColor method for them. http://developer.android.com/reference/android/view/View.html#setBackgroundTintList(android.content.res.ColorStateList) – Alexander N. Mar 29 '16 at 19:05
  • For example if I want to change LinearLayout color I should give it id, find it by id in .java file and then use object.setColor? I have a lot of object so I have to use setcolor for all of them every time application starts? – littlewombat Mar 29 '16 at 19:13
  • Yes. That would be how I would do it. – Alexander N. Mar 30 '16 at 16:06
  • It's not `setColor(String)`. It's `parseColor(String)` – Prabs Mar 24 '17 at 05:45
  • @Prabs - I think that depends on what type of object that you are using. Since no specific object is specified here, setColor can be used. In some instances, parseColor might be a better option to use, however, since this is just an arbitrary sample, ```setColor(String color)``` is valid – Alexander N. Mar 24 '17 at 16:06
  • @AlexanderN. How can you store a hexadecimal backgroundcolor string in sharedpref? Can you explain? – Anthon Santhez Dec 28 '20 at 09:26
0

if the user give you hex string ( as you answer to NoChinDeluxe) you should store it in string in your sharePref and then parse it with :

public static int parseColor (String colorString)

I always do my Prefs like this:

private static final String KEY_COLOR_1 = "color 1";

private static Prefs instance;

public static Prefs with(Context ctx) {
    if (instance == null) {
        instance = new Prefs();
    }
    instance.ctx = ctx.getApplicationContext();
    return instance;
}

private Context ctx;

private Prefs() {
}

public SharedPreferences getPrefs() {
    return PreferenceManager.getDefaultSharedPreferences(ctx);
}
public String getColor1() {
        return getPrefs().getStrings(KEY_COLOR_1, "");
    }

    public void setColor1(String color) {
        getPrefs().edit().putStrings(KEY_COLOR_1, color).apply();
    }

and then you can get your color with:

Pref.with(this).getColor1();
JCDecary
  • 557
  • 1
  • 7
  • 18
  • So class appdata is correct? Can you give me a hint how to change the sharedpreferences value and how to use it in a xml file? – littlewombat Mar 29 '16 at 18:55