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.
