Color Resources

Description

You can use reference identifiers to indirectly reference colors.

The color IDs are available under the your-package.R.color namespace.

Android defines a base set of colors, which are accessible through the Android android.R.color namespace.

Example

The following shows some examples of specifying color in an XML resource file.


<resources>
    <color name="red">#f00</color>
    <color name="blue">#0000ff</color>
    <color name="green">#f0f0</color>
    <color name="main_back_ground_color">#ffffff00</color>
</resources>

The entries above need to be in a file residing in the /res/values subdirectory.

The name of the file can be anything you choose.

The following code shows an example of using a color resource in Java code.


int mainBackGroundColor =  activity.getResources.getColor(R.color.main_back_ground_color);

main_back_ground_color is defined in the resource file above.





















Home »
  Android »
    Android Basics »




Hello Android
Resources
Activity
Calendar
Camera
Contact
Content Provider
Database
Hardware
Intent
Location
Media
Network
Notification
Preference
Sensor
Service
SMS