Open In App

Android | res/values folder

Improve
Improve
Like Article
Like
Save
Share
Report

The res/values folder is used to store the values for the resources that are used in many Android projects to include features of color, styles, dimensions etc.

Below explained are few basic files, contained in the res/values folder:

  1. colors.xml: The colors.xml is an XML file which is used to store the colors for the resources.

    An Android project contains 3 essential colours namely:

    • colorPrimary
    • colorPrimaryDark
    • colorAccent

    These colors are used in some predefined resources of the android studio as well. These colors as needed to be set opaque otherwise it could result in some exceptions to arise.

    Below mentioned is the implementation of colors.xml resource:




    <?xml version="1.0" encoding="utf-8"?>
    <resources>
        <color name="colorPrimary">#1294c8</color>
        <color name="colorPrimaryDark">#1294c8</color>
        <color name="colorAccent">#FF4081</color>
      
        <color name="text_color">#555555</color>
      
      
        <color name="colorText">#FFFFFF</color>
        <color name="colorTextHint">#51d8c7</color>
    </resources>

    
    

    Note: It is also possible to define different user based colours for different types of resources.

  2. dimens.xml: The dimens.xml is used for defining the dimensions for different widgets to be included in the Android project. It is a good coding practice to use dimens.xml to define a dimension rather than just writing the dimension in the resource, due to the fact that if ever any need arises to change the dimension, instead of making a change to all, only the dimens.xml can be changed once and the change is reflected in all.
    Below mentioned is the implementation of dimens.xml resource:




    <resources>
        <!-- Default screen margins, per the Android Design guidelines. -->
        <dimen name="activity_horizontal_margin">16dp</dimen>
        <dimen name="activity_vertical_margin">16dp</dimen>
        <dimen name="nav_header_vertical_spacing">8dp</dimen>
        <dimen name="nav_header_height">176dp</dimen>
        <dimen name="fab_margin">16dp</dimen>
    </resources>

    
    

    It is also possible to apply user-defined dimensions.
    Note: Always remember the difference in using dp or sp. Generally use sp for font size and dp for others.

  3. strings.xml: One of the most important as well as widely used values file is the strings.xml due to its applicability in the Android project. Basic function of the strings.xml is to define the strings in one file so that it is easy to use same string in different positions in the android project plus it makes the project looks less messy.
    We can also define an array in this file as well.
    Below mentioned is the implementation of strings.xml resource:




    <resources>
        <string name="app_name">Workshop app</string>
      
        <string name="navigation_drawer_open">Open navigation drawer</string>
        <string name="navigation_drawer_close">Close navigation drawer</string>
        <string name="action_settings">Settings</string>
        <string name="hello_blank_fragment">Hello blank fragment</string>
        <string name="date">Date:</string>
        <string name="timings">Timings:</string>
    </resources>

    
    

    Android studio gives a warning in layout xml if a string is used in that file, thus it is a good practice to store all hardcoded strings in strings.xml file.

  4. styles.xml: Another important file in the values folder is the styles.xml where all the themes of the Android project are defined. The base theme is given by default having the option to customize or make changes to the customized theme as well. Every theme has a parent attribute which defines the base of the theme. There are a lot of options to choose from depending on the need of the Android project.
    Below mentioned is the implementation of styles.xml resource:




    <resources>
      
        <!-- Base application theme. -->
        <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/colorAccent</item>
        </style>
      
        <style name="AppTheme.NoActionBar">
            <item name="windowActionBar">false</item>
            <item name="windowNoTitle">true</item>
        </style>
      
        <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
      
        <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
      
    </resources>

    
    

    If any feature used in the files in values folder does not match with the minimum SDK version of the user, then android studio gives the option to define a separate file with the same name but for different API level. For eg., styles and styles(v21)[for API levels of 21 and above].



Last Updated : 07 Jul, 2022
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads