0

I wanted to create a @color/ color value that can be changed programmatically, but I don't know how to do.
Here is my code (color/color_account.xml):

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/colorId" android:color="#"/>
</selector>

But the id isn't accepted and I can't use it as a drawable (logical).
Can someone explain me how to do?

The colors are not defined, they are chosen by the users so it must be changed programmatically

4
  • Does this answer your question? How to change pressed color to other color of selector dynamically? Commented Feb 27, 2021 at 12:02
  • No, I want a personnalised theme, not a changed color with an event. I must be able to change all the views with the default color by the user color. Sorry if I'm not very clear
    – Kaki In
    Commented Feb 27, 2021 at 12:08
  • Finally I change all the text colors and the shape solid color too, but if it is a better way I'm always interested. If there is vars in css, why not in xml styles?
    – Kaki In
    Commented Feb 28, 2021 at 18:09
  • Ok it doesn't works, sorry, my shape color isn't modifiable because it is in a selector with another shape
    – Kaki In
    Commented Mar 1, 2021 at 14:28

2 Answers 2

1

If i'm getting your question right you want to change a color in colors.xml programmatically. And sadly because all the colors in that file are static final ints you can not change them. You can either set new resource value in there or set a color out of your colors.xml with code.

11
  • But can I change a color outside colors.xml but always named @color/colorAccount?
    – Kaki In
    Commented Feb 27, 2021 at 14:04
  • when u say @color it is referring to colors.xml on your project so it is impossible to change a value of it if its in annotation of color or have separated values with same names. if your gonna change the colors with the users choice i recommend to do that with color picker. either making it yourself or use the color picker libraries. then initialize any view you are going to change color with and set the whatever color of that view to the selected color by the user. Commented Feb 27, 2021 at 15:07
  • also the idea of using theme for handling the color of views in general is ofcourse a good idea but there are some needs that make some one to use a color picker or just changing some views colors without themes that is up to you to decide which one you want Commented Feb 27, 2021 at 15:09
  • But a color picker is an input, it isn't? I just have to change an @color/colorId value to change the whole color of the application theme, but to chose the color it is already possible, no need about that
    – Kaki In
    Commented Feb 28, 2021 at 17:56
  • 1
    its totally impossible for that to happen Commented Mar 19, 2021 at 19:15
1

Create a new file named colors.xml in res/values and add your color codes in that xml.

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="mycolor">#FFFFFF</color>
</resources>

Now when you want to use that color you can do @color/mycolor

5
  • 1
    Sorry but I want to change this color with a personnalised another color, so I can't use this method
    – Kaki In
    Commented Feb 27, 2021 at 12:03
  • In this case, you can try using themes. This post has the similar problem, maybe it might help. stackoverflow.com/questions/50136548/…
    – Eagleclaw
    Commented Feb 27, 2021 at 12:18
  • 1
    I read, but I don't find how to change a color programmatically (sorry if I'm repeating). It explain how to change the color with another defined color, but mine must be chosen by the user, and I will not write the 16777216 colors existing, so I must change an attribute of a @color/colorAccount with an id or something like that
    – Kaki In
    Commented Feb 27, 2021 at 12:38
  • Well since you want user the choose color while program is running, you can get color code with a color picker used by the user. And change the color of element you want to change. But either way, if user chooses the color from xml or from color picker, you have to save the colorcode so you can use it later. If so, why not just save the colorcode without xml? It might not the answer you're looking for but this is best i can think of. Sorry for cant helping you more. Good luck.
    – Eagleclaw
    Commented Feb 27, 2021 at 12:47
  • 1
    But I need to change a shape color, so I can't use the background attribute because it will remove the drawable that I use. The colorcode is already saved but I just need to use it for a drawable color
    – Kaki In
    Commented Feb 27, 2021 at 14:07

Not the answer you're looking for? Browse other questions tagged or ask your own question.