Space Ghost
Space Ghost

Reputation: 765

How do I use my own colors in android XML

I have an XML file I created in the res/value folder and try to reference it by writing

android:background="@colors/red"

and it does not work.

I also tried placing this file in the drawable folder but to no avail.

Upvotes: 0

Views: 101

Answers (5)

Swapnil
Swapnil

Reputation: 654

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <color name="white">#FFFFFF</color>
 <color name="yellow">#FFFF00</color>
 <color name="fuchsia">#FF00FF</color>
 <color name="red">#FF0000</color>
 <color name="silver">#C0C0C0</color>
 <color name="gray">#808080</color>
 <color name="olive">#808000</color>
 <color name="purple">#800080</color>
 <color name="maroon">#800000</color>
 <color name="aqua">#00FFFF</color>
 <color name="lime">#00FF00</color>
 <color name="teal">#008080</color>
 <color name="green">#008000</color>
 <color name="blue">#0000FF</color>
 <color name="navy">#000080</color>
 <color name="black">#000000</color>
</resources>

make one xml named colors.xml in res and add color names and codes as you want custum

Upvotes: 0

Adhikari Bishwash
Adhikari Bishwash

Reputation: 2780

easy way

android:background="#8EE8B5"

Upvotes: 1

FD_
FD_

Reputation: 12919

  • Your color res file has to be under res/values/ (You may add it to a qualifier res/values folder though)
  • Add the color in this format:

    <resources>
        <color name="white">#ffffff</color>
    </resources>
    
  • Refer to it using @color/white.

Upvotes: 2

user3317558
user3317558

Reputation: 482

You can declare a color in your color.xml file like below,

<color name="aqua_blue">#6495ED</color>

And then use it below,

android:background="@color/aqua_blue"

Upvotes: 1

laalto
laalto

Reputation: 152817

Refer to color resources with @color/name, not @colors/name.

For example, in res/values/whatever.xml:

<color name="your_color_name">#12345678</color>

Then in a layout xml:

android:background="@color/your_color_name"

Upvotes: 3

Related Questions