27

Android programming is driving me crazy. The XML or programmable ways for GUI development is making a right old dogs breakfast of the code--some stuff here, some stuff there.

My current frustration is trying to keep it all XML and I would like to set the background color of the TextView the same color as the background color of a Button without having to specify the exact color code. I would like to use a system constant, something like java.awt.SystemColor.control, but specified in XML.

Is there anyway of doing this without having to respecify a bunch of stuff? I'm looking for a solution like: android:background="{Insert constant here}".

toniedzwiedz
  • 17,895
  • 9
  • 86
  • 131
zam664
  • 757
  • 1
  • 7
  • 18

5 Answers5

38

You should have a file in res/values called colors.xml, it should look like this:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="white">#FFFFFF</color>
    <color name="grey">#7A7A7A</color>
    <color name="background">#0044AA</color>
    <color name="bluelight">#449DEF</color>
    <color name="bluedark">#2F6699</color>
    <color name="greenlight">#70C656</color>
    <color name="greendark">#53933F</color>
    <color name="orangelight">#F3AE1B</color>
    <color name="orangedark">#BB6008</color>
    <color name="black">#000000</color>
</resources>

Then you can just call the color in xml like this:

android:background="@color/greenlight"
Matt Harris
  • 3,496
  • 5
  • 32
  • 43
  • 5
    But I do not want to specify the color. I want to specify a system style. – zam664 Dec 30 '11 at 14:18
  • 2
    This is generally the preferred way to do it, as there aren't many system defined colours, however you can use android:background="@android:color/white". You can find a full list here: http://developer.android.com/reference/android/R.color.html – Matt Harris Dec 30 '11 at 14:22
  • I have a color constant for ex : 17170449 which I need to convert into the hexa decimal code. is it possible? Thanks in advance @MattHarris – Ashokchakravarthi Nagarajan Jun 10 '14 at 13:32
27

You can use things like "@color/white", or for android system constants, "@android:color/holo_red_light". Alternatively if you want more control, you can always define a common background in a drawable and set the background to "@drawable/mydrawable"

List of resources available for android:color here (like background_dark): http://developer.android.com/reference/android/R.color.html

Joel Martinez
  • 46,929
  • 26
  • 130
  • 185
  • 3
    This is the problem that I am having with Android. I do not want to have to redefine everything myself. I want to use standards on the system. I don't care what those standards are I just want a similar user look and feel. – zam664 Dec 30 '11 at 14:21
  • That's precisely why you should use android:color/ ... there's lots of existing presets that aren't color specific and will change based on the manufacturer. updated answer with link – Joel Martinez Dec 30 '11 at 14:25
  • 1
    Just a heads up, your example is incorrect, there is no "@android:color/red" – jt-gilkeson Mar 28 '14 at 19:07
  • And correct you are @RedLenses ... updated the answer to be one of the valid values from the documentation :) – Joel Martinez Apr 05 '14 at 00:33
  • Hi @JoelMartinez can you please show example of using a small image as a repeat in the background. I did it like this - https://gist.github.com/Noitidart/be2857ca33e83498ab34b78c13a48ca1 - it doesnt work right for me. – Noitidart Aug 10 '17 at 14:37
3

check out this page: http://android-er.blogspot.com/2010/03/using-color-in-android.html

You can use: "@android:color/white" in your xml. I imagine that a few other common colors are supported, but I don't know for sure which ones.

The preferred solution is generally to create your own colors.xml resource file and reference the ones that you put inside there.

FoamyGuy
  • 46,603
  • 18
  • 125
  • 156
1

used like this : android:background="#FF00FF"

Hiren Dabhi
  • 3,693
  • 5
  • 37
  • 59
0

You don't need using xml file to create a constants' XML,

it can help you: Java constants file

Community
  • 1
  • 1
Franklin Hirata
  • 290
  • 2
  • 12
  • 1
    I want to do it in XML not Java. I can get the colors via Java programming but the intention is to the use XML for the GUI design and have that all in one location. I don't want half my GUI in XML and half in Java. – zam664 Dec 30 '11 at 19:21