Android | res/values folder

0

Last Updated : 22 Apr, 2024

The res/values folder is used to store the values for the resources that are used in many Android projects including features of color, styles, dimensions, etc. In this article, we will learn about the res/values folder.

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

  • colors.xml
  • dimens.xml
  • strings.xml
  • styles.xml

1. colors.xml

The colors.xml is an XML file that is used to store the colors for the resources. An Android project contains 3 essential colors namely:

  • colorPrimary
  • colorPrimaryDark
  • colorAccent

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

Below mentioned is the implementation of the colors.xml resource:

colors.xml

         name="colorPrimary">#1294c8       name="colorPrimaryDark">#1294c8       name="colorAccent">#FF4081        name="text_color">#555555         name="colorText">#FFFFFF       name="colorTextHint">#51d8c7    

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:

dimens.xml

             name="activity_horizontal_margin">16dp       name="activity_vertical_margin">16dp       name="nav_header_vertical_spacing">8dp       name="nav_header_height">176dp       name="fab_margin">16dp    

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:

strings.xml

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

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:

styles.xml

             name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">                     name="colorPrimary">@color/colorPrimary           name="colorPrimaryDark">@color/colorPrimaryDark           name="colorAccent">@color/colorAccent              name="AppTheme.NoActionBar">           name="windowActionBar">false           name="windowNoTitle">true              name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />       name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />    

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].

News

Improve

Leave A Reply

Your email address will not be published.