Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android System Color Constants

Tags:

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}".

like image 948
zam664 Avatar asked Dec 30 '11 14:12

zam664


People also ask

What color formats are supported for color resources in android?

red(int) to extract the red component. green(int) to extract the green component. blue(int) to extract the blue component.

What is the color of android?

The official Android color is green.

What is primary color and secondary color android?

The primary color is the color displayed most frequently across your app's screens and components. The primary variant color is used to distinguish two elements of the app using the primary color, such as the top app bar and the system bar. The secondary color provides more ways to accent and distinguish your product.

What color space does android use?

The one most important to Android is sRGB. Before Android Oreo, applications used the sRGB color space. There's a reason for this — low-end hardware. Displaying a wide color gamut takes more GPU and CPU power than the sRGB space.


2 Answers

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" 
like image 111
Matt Harris Avatar answered Oct 05 '22 14:10

Matt Harris


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

like image 24
Joel Martinez Avatar answered Oct 05 '22 15:10

Joel Martinez