[TinyTut] - Colors as XML-Resources

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

[TinyTut] - Colors as XML-Resources

Postby plusminus » Thu Dec 06, 2007 5:14 pm

[align=center][TinyTut] - Colors as XML-Resources[/align]

What you learn: You will learn how to specify Colors in an XML-Resource and how to use them in other xml-files and how to read them out to your code.

Difficulty: 1 of 5 :)

:idea: Questions/Problems: Simply post below...

Description:
Defining colors in Javacode is not nice and not really flexible :!:
Colors should be defined in a file called (i.e.) colors.xml in your project folder: /res/values/".

There are various possibilities of describing a color. All at least contain a HexChar for R(ed), G(reen) and B(lue). Before those values there can be an A(lpha), what is the Transparency of the Color.

So all possibilities are:
  • #RGB
  • #ARGB
  • #RRGGBB
  • #AARRGGBB

So a bright strong red would be #F00 or #FF00 or #FF0000 or #FFFF0000[/list].
Note: F is the highest of the Hex-Chars and 0 the lowest.

Example-File from the Christmas-Calendar-Tutorial:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.    <color name="door_background">#4200</color>
  6.  
  7.    <color name="door_caption_color_selected">#F00</color>
  8.  
  9.    <color name="door_caption_color">#BBFF0000</color>
  10.  
  11. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


Use them in other xml-Files (like style.xml or layout.xml) like the following:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2.  
  3. <resources>
  4.  
  5.     <style name="DoorButton">
  6.  
  7.         <!-- Replaces the complex BackGround to a simple color -->
  8.  
  9.         <item name="android:background">@color/door_background</item>
  10.  
  11.         <!-- Text-Color -->
  12.  
  13.         <item name="android:textColor">@color/door_caption_color_selected</item>
  14.  
  15.         <item name="android:normalTextColor">@color/door_caption_color</item>
  16.  
  17.     </style>
  18.  
  19. </resources>
Parsed in 0.002 seconds, using GeSHi 1.0.8.4


To read out the Color to your Java-Code do the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. int color = getResources().getColor(R.color.door_caption_color_selected);
Parsed in 0.029 seconds, using GeSHi 1.0.8.4


Regards,
plusminus
Image
Image | Android Development Community / Tutorials
User avatar
plusminus
Site Admin
Site Admin
 
Posts: 2688
Joined: Wed Nov 14, 2007 8:37 pm
Location: Schriesheim, Germany

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests