How To Define Custom Color Variables In Android Studio

The android class android.graphics.Color provides some color constants which can be used directly in an android application. For example, BLACK, WHITE, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, GRAY, LTGRAY (light gray), DKGRAY (dark gray). But you may find that it is not enough, sometimes you need more colors. So this article will show you how to define custom color variables in android studio.

1. Custom Color Variable Advantages.

Define and use the color variable instead of color value such as “#112233” in java code or XML file has below advantages.

  1. Easier to remember: You can assign a more meaningful name to a color value. Such as backgroundColorRed, from this variable name we can see that this variable is not only represented the red color but also represent a background color.
  2. Easier to maintain: Suppose you use one color value in a lot of places such as java source code, XML files, etc. If you want to change the color value, you should change so many files. If you use a defined color variable, you just need to change the color definition file in one place.

2. How To Define A Custom Color Variable.

  1. Go to android studio Project View —> Android Subview.
  2. Right-click app/res/values folder. Click New —> Values resource file menu item to popup the New Resource File dialog window.
  3. In the New Resource File dialog window, input keyword colors in the File name input box, select main in the Source set drop-down list,  input values in the Directory name input text box, then click the OK button.
  4. Then colors.xml file will be created in the left panel under the app/res/values folder. Double click it, input below color definition XML data in it. The name attribute value ( for example “colorRed” ) will be used as a variable name referred to in other XML files or java source codes. #f1a86c is just the color RGB value that the color variable represents.
    <?xml version="1.0" encoding="utf-8"?>
    <resources>
       <color name="colorRed">#ff0000</color>
       <color name="colorOrange">#f1a86c</color>
    </resources>
  5. For each color variable, there is a color block at the line beginning. Click it, there will popup Choose Color dialog which can make modify color more easily.

3. How To Use Custom Color Variable Defined In Colors.xml.

The custom color variations can be used in both XML or java files. It is easy to refer them.

  1. Use the custom color variable in an XML file such as the layout XML file example. @color/colorVariableName @color/colorOrange.
    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:background="@color/colorRed" />
    
    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView"
        android:background="@color/colorOrange" />
  2. Use the custom color variable in the java file example. R.color.colorVariableName R.color.colorRed. R is the android auto-created resource class.
    final int[] colorArr = new int[];
    {
        R.color.colorRed, R.color.colorOrange
    }
0 0 votes
Article Rating
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x