0

I currently defined my colors as constants, for example:

public static final int BG_COLOR = Color.parseColor("#262525");

I want to better my life and define all colors in my colors.xml file, for example:

<?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="bgcolor">#3F51B5</color>
    </resources>

Is there an easy refactoring tool to do this with a single click?

Just to clarify: I do know how the resource file works, I also now how to do the coding. So, this is NOT a duplicate as suggested. What I am asking is, is if there is an easy refactor tool to convert colors, defined as constants in my class file, to colors in my xml file.

Now if I would change it, I would add the color to the xml, go through all my code, see where the color is used and change my code. This is fine and this works and I know how to do this. The thing is, it is a manual process, especially when a lot of different objects are used and a lot of different colors are being used.

I am therefore wondering if there is a simpler way to do this, a refactoring tool that does the work for me. I am looking for something similar as what you can do in Android Studio with Strings in an XML-file. In that case, if you hardcode a string, you have the option to move it to the strings.xml file by using the option "Extract string resource".

MWB
  • 1,830
  • 1
  • 17
  • 38
  • Not really the same. What I try to do is refactor my code. I have now defined the color as a constant in my class. I want to use the resources file instead. – MWB Jan 13 '18 at 10:50
  • 2
    I do not think there is refactoring for this AFAIK. – ADM Jan 13 '18 at 10:54

2 Answers2

-1

see this link all Material design color Written as resource ! you can copy all colors and save to res/colors.xml and use of each project.

Community
  • 1
  • 1
  • Thanks for the advice. It down not answer my question, though. I am looking for an easy way to refactor. – MWB Jan 13 '18 at 10:53
  • 1
    if your mean is how to extract color code in java code and save into colors.xml best and easy way its copy / paste ! –  Jan 13 '18 at 15:43
  • That is exactly what I mean. Sorry to hear there is no easy refactor tool. Perhaps a nice suggestion for Android Studio. – MWB Jan 14 '18 at 10:20
-1

After defining color in colors.xml in layout use android:textColor="@color/bgcolor"

vkrams
  • 7,267
  • 17
  • 79
  • 129
Kapil Parmar
  • 881
  • 8
  • 19
  • I know how the coding works. This is a refactoring question. I have updated the question. – MWB Jan 13 '18 at 10:52