Breaking News
Home / Android / Android Snippets / How do you define Colors Resources in Android?

How do you define Colors Resources in Android?

Answer

Create an xml file in the res/values directory called as colors.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<resources>
    <color name=”White”>#FFF</color>
    <color name=”Black”>#000</color>
    <color name=”Grey”>#7F7F7F</color>
    <color name=”DarkGrey”>#4F4F4F</color>
    <color name=”Green”>#0F0</color>
    <color name=”TransparentGrey”>#7F000000</color>
</resources>   

You can define what ever colors you want as resources as it is shown above. Once defined, the Android compiler will automatically create resources identifiers in the R file that you can refer in your code as show below:

  public static final class color {
        public static final int Black=0x7f060001;
        public static final int DarkGrey=0x7f060003;
        public static final int Green=0x7f060004;
        public static final int Grey=0x7f060002;
        public static final int TransparentGrey=0x7f060005;
        public static final int White=0x7f060000;
    }

<!>

[pb_builder]

Check Also

How to check if an Android device can connect to Internet?

The following code snippet shows how to check if an Android device can connect to …

Advertisment ad adsense adlogger