Skip to content

The values/ Folder in Android

The values/ folder in an Android project contains XML files that store reusable resources like strings, colors, dimensions, and styles. Instead of hardcoding values in layouts or Java/Kotlin files, we define them in values/ for maintainability, consistency, and localization support.


📂 Location of the values/ Folder

app/src/main/res/values/
│── strings.xml
│── colors.xml
│── dimens.xml
│── styles.xml
│── themes.xml
│── arrays.xml
│── bools.xml
│── integers.xml

Each XML file serves a specific purpose, which we’ll discuss in detail below.


🔹 1. strings.xml (Text Storage for Localization) 📝

  • Stores app-wide text such as button labels, messages, and titles.
  • Supports multiple languages for localization.

📌 Example: res/values/strings.xml

<resources>
<string name="app_name">MyApp</string>
<string name="welcome_message">Welcome to MyApp!</string>
<string name="login_button">Login</string>
</resources>

📌 How to Use Strings in Code?

<TextView
android:text="@string/welcome_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

String welcome = getString(R.string.welcome_message);

📌 Localization Example (French Translation – res/values-fr/strings.xml)

<resources>
<string name="welcome_message">Bienvenue sur MyApp!</string>
</resources>

🔹 Best Practice: Never hardcode text in layouts or code. Always use strings.xml.


🔹 2. colors.xml (Color Definitions) 🎨

  • Stores app-wide color values for UI consistency.

📌 Example: res/values/colors.xml

<resources>
<color name="primaryColor">#6200EE</color>
<color name="secondaryColor">#03DAC5</color>
<color name="textColor">#FFFFFF</color>
</resources>

📌 How to Use Colors?

<TextView
android:textColor="@color/textColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

textView.setTextColor(getResources().getColor(R.color.textColor));

🔹 Best Practice: Define colors in colors.xml instead of hardcoding them in layouts.


🔹 3. dimens.xml (Dimensions for UI Elements) 📏

  • Stores padding, margins, text sizes, and layout dimensions.

📌 Example: res/values/dimens.xml

<resources>
<dimen name="text_size">16sp</dimen>
<dimen name="padding_large">20dp</dimen>
<dimen name="margin_small">8dp</dimen>
</resources>

📌 How to Use Dimensions?

<TextView
android:textSize="@dimen/text_size"
android:layout_margin="@dimen/margin_small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

float textSize = getResources().getDimension(R.dimen.text_size);

🔹 Best Practice: Use dimens.xml to make the UI responsive across different screen sizes.


🔹 4. styles.xml (Reusable UI Styling) 🎭

  • Defines themes and UI styles for buttons, text, layouts, etc.

📌 Example: res/values/styles.xml

<resources>
<style name="CustomButton" parent="Widget.MaterialComponents.Button">
<item name="android:background">@color/primaryColor</item>
<item name="android:textColor">@color/textColor</item>
<item name="android:padding">10dp</item>
</style>
</resources>

📌 How to Apply Styles?

<Button
style="@style/CustomButton"
android:text="Submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

🔹 Best Practice: Use styles.xml to keep UI designs consistent and reusable.


🔹 5. themes.xml (App-Wide Theme Customization) 🌗

  • Defines app themes, light/dark mode settings, and global styles.

📌 Example: res/values/themes.xml (Light Theme)

<resources xmlns:tools="http://schemas.android.com/tools">
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Light">
<item name="colorPrimary">@color/primaryColor</item>
<item name="android:textColorPrimary">@color/textColor</item>
</style>
</resources>

📌 Dark Mode (res/values-night/themes.xml)

<resources>
<style name="Theme.MyApp" parent="Theme.MaterialComponents.Dark">
<item name="colorPrimary">@color/secondaryColor</item>
</style>
</resources>

🔹 Best Practice: Use themes.xml for dynamic theming in dark mode and UI customization.


🔹 6. arrays.xml (Lists of Values) 📋

  • Stores lists of values, like dropdown options, list items, etc.

📌 Example: res/values/arrays.xml

<resources>
<string-array name="countries">
<item>USA</item>
<item>India</item>
<item>Canada</item>
</string-array>
</resources>

📌 How to Use in Spinner?

<Spinner
android:entries="@array/countries"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

String[] countries = getResources().getStringArray(R.array.countries);

🔹 Best Practice: Use arrays.xml for dropdown menus, list options, and multi-language support.


🔹 7. bools.xml (Boolean Values) ✅

  • Stores true/false values for feature toggles or settings.

📌 Example: res/values/bools.xml

<resources>
<bool name="is_feature_enabled">true</bool>
</resources>

boolean isFeatureEnabled = getResources().getBoolean(R.bool.is_feature_enabled);


🔹 8. integers.xml (Integer Values) 🔢

  • Stores integer values for counters, limits, and settings.

📌 Example: res/values/integers.xml

<resources>
<integer name="max_retry_count">3</integer>
</resources>

int maxRetry = getResources().getInteger(R.integer.max_retry_count);


🚀 Summary: Why Use values/ Instead of Hardcoding?

Easier Maintenance – Update values in one place.
Supports Localization – Translate text into multiple languages.
Consistency in UI – Reuse colors, styles, and dimensions.
Better Code Organization – Keeps layout XML files clean and modular.