I have a colors.xml
in res/value
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#3F51B5</color>
<color name="colorPrimaryDark">#303F9F</color>
<color name="colorAccent">#FF4081</color>
<color name="indicator_unselected">#4D000000</color>
<!--Indigo 500-->
<color name="primary_500">#3F51B5</color>
<!--Indigo 700-->
<color name="primary_700">#303F9F</color>
<!--Pink 500-->
<color name="accent_500">#E91E63</color>
<!--Pink 700-->
<color name="accent_700">#C2185B</color>
<color name="black_trans80">#33000000</color>
<color name="blue_grey">#455A64</color>
<color name="red">#F44336</color>
<color name="pink">#E91E63</color>
<color name="purple">#9C27B0</color>
<color name="deep_purple">#673AB7</color>
<color name="indigo">#3F51B5</color>
<color name="blue">#2196F3</color>
<color name="light_blue">#03A9F4</color>
<color name="cyan">#00BCD4</color>
<color name="teal">#009688</color>
<color name="green">#4CAF50</color>
<color name="light_green">#8BC34A</color>
<color name="lime">#CDDC39</color>
<color name="yellow">#FFEB3B</color>
<color name="amber">#FFC107</color>
<color name="orange">#FF9800</color>
<color name="deep_orange">#FF5722</color>
<color name="brown">#795548</color>
<color name="grey">#E0E0E0</color>
<color name="white_70">#B3FFFFFF</color>
<color name="cyan_50">#E0F7FA</color>
<color name="amber_50">#FFF8E1</color>
<color name="purple_50">#F3E5F5</color>
</resources>
When I want to use any of these colors in a layout, let's take listview.xml
(a layout that contains a listview).
I have a LinearLayout and a ListView in it. So, If I want to change the background color of the LinearLayout I have to do something like this:
android:background="@color/cyan"
This works. My question is: My file is called colors.xml not color.xml Why can I still access these custom colors by using @color? Why is @colors giving me an error?
File name doesn't matter, thing matter is a tag. You can create a file by your name and can place colors in that. But make sure they are in <color>
tag. We generally follow this convention and save our colors in colors.xml even you can save your colors in palette.xml.
<color name=</color> // This tag matter and actually define resources
// It doesn't matter what is a file name
In your case you are accessing your color resource by its name which is cyan. We never specify in which file it resides.
<color name="cyan">#00BCD4</color>