Friday, October 16, 2015

Working with 6-digit(RGB) and 8-digit hex colors (ARGB) in android

RGB and ARGB: 
        RGB is a 3-channel format containing data for Red, Green, and Blue. ARGB is a 4-channel format containing data for Alpha, Red, Green and Blue .
There is no use to the Alpha channel other than making the color transparent/opaque (or partially transparent; translucent).
You might find an RGB value packed into 16 bits, with 5 bits for Blue and Red, and 6 bits for Green (green gets more bits because the eye is more discerning to shades of green). You might also find an RGBA value packed into 16 bits, with 5 bits for each color and 1 bit for alpha. With one bit, you can only make the color fully transparent or not transparent at all.
Typically nowadays, you'll find RGB and RGBA packed into 32 bit values, with 8 bits for each color, and 8 bits for alpha (or left blank for RGB).

Use in Android:
A color value defined in XML. The color is specified with an RGB value and alpha channel. You can use a color resource any place that accepts a hexadecimal color value. You can also use a color resource when a drawable resource is expected in XML (for example, android:drawable="@color/green").
The value always begins with a pound (#) character and then followed by the Alpha-Red-Green-Blue information in one of the following formats:
  • #RGB
  • #ARGB
Note: A color is a simple resource that is referenced using the value provided in the name attribute (not the name of the XML file). As such, you can combine color resources with other simple resources in the one XML file, under one <resources> element.
The filename is arbitrary. The <color> element's name will be used as the resource ID.
In Java: R.color.color_name
In XML: @[package:]color/color_name
<?xml version="1.0" encoding="utf-8"?>
Required. This must be the root node.
No attributes.
A color expressed in hexadecimal, as described above.
String. A name for the color. This will be used as the resource ID.
XML file saved at res/values/colors.xml:
<?xml version="1.0" encoding="utf-8"?>
   <color name="opaque_red">#f00</color>
   <color name="translucent_red">#80ff0000</color>
This application code retrieves the color resource:
Resources res = getResources();
int color = res.getColor(R.color.opaque_red);
This layout XML applies the color to an attribute:
Reference :


  1. this is really too useful and have more ideas from yours. keep sharing many techniques. eagerly waiting for your new blog and useful information. keep doing more.
    school websites uk

  2. thanks for shared wonderful information of giving best information.its more useful and more helpful. great doing keep sharing
    GMAT coaching chennai



Copyright @ 2014 Tech Tutorial .