Skip to content
Home ยป The values/ Folder in Android

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.