Difference between res/color and res/values/colors.xml in Android resources folder

18,208

Solution 1

See Color State List Resource

Did you follow that link? http://developer.android.com/guide/topics/resources/color-list-resource.html

I think it answers your question.

Solution 2

By location,

res/color/

Is for the resource that is compiled to datatype Resource pointer to a ColorStateList.

  • A ColorStateList is an object you can define in XML that you can apply as a color, but will actually change colors, depending on the state of the View object to which it is applied.

  • syntax:

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android" >
       <item
        android:color="hex_color"
        android:state_pressed=["true" | "false"]
        android:state_focused=["true" | "false"]
        android:state_selected=["true" | "false"]
        android:state_checkable=["true" | "false"]
        android:state_checked=["true" | "false"]
        android:state_enabled=["true" | "false"]
        android:state_window_focused=["true" | "false"] />
      </selector>
    

res/values/

If you want to provide a static color resource, use a simple Color value.

  • That is a color value defined in XML, specified with an RGB value and alpha channel.
  • You can use a color resource any place that accepts a hexadecimal color value.
  • You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green").
Share:
18,208

Related videos on Youtube

Marek
Author by

Marek

Helped? :) BTC tips 1Nq3SS8QA7gCaSLxkmfkThZbXUsbLBRsZJ

Updated on October 31, 2020

Comments

  • Marek
    Marek over 3 years

    Is there any reason, why in the resources folder we have two folders in which we can define colors? (according to android developer page http://developer.android.com/guide/topics/resources/providing-resources.html#ResourceTypes).

    This is the quote from android developer page:

    values/
    XML files that contain simple values, such as strings, integers, and colors.

    color/
    XML files that define a state list of colors. See Color State List Resource

    Is there any difference between Colors stored in res/colors and res/values? Which one is more preferable?

    • Chor Wai Chun
      Chor Wai Chun almost 11 years
      In very general form, you can refer /values as a fix color, eg: black = #000000, so you can call "black" in layout instead of writing "#000000". While /color gives you the option to have different color on different view stat, eg: different color on disabled and enabled button.