Junior
What are resources in Android and what data can be stored in them?
sobes.tech AI
Answer from AI
Resources in Android are external data used by the application but not included directly in the code. They are separated from the application's logic and stored in subdirectories of the res folder in the project's root directory. Android provides a set of tools for accessing and managing resources.
Resources are used for:
- Localization of the application (supporting different languages).
- Adaptation to different screen sizes and resolutions.
- Separating design from logic.
- Simplifying interface updates.
The following types of data can be stored in resources:
- Drawable: Various graphic elements such as images (PNG, JPEG, GIF), XML shape definitions, switches, etc. Stored in
drawablesubdirectories.<shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <solid android:color="#FF0000"/> <corners android:radius="10dp"/> </shape> - Layout: XML files describing the structure of the user interface for Activity, Fragment, or View. Stored in
layoutsubdirectories.<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hello, world!"/> </LinearLayout> - Mipmap: Optimized images for app icons of various densities. Preferable for icons over
drawable. Stored inmipmapsubdirectories (e.g.,mipmap-hdpi,mipmap-xhdpi). - String: String values used in the app. Facilitate easy localization of text. Stored in XML files in
valuessubdirectories (e.g.,values/strings.xml).<resources> <string name="app_name">My Application</string> <string name="welcome_message">Welcome!</string> </resources> - Style: Style definitions for Views. Allow centralized management of appearance attributes. Stored in XML files in
valuessubdirectories (e.g.,values/styles.xml).<resources> <style name="MyTextViewTextAppearance" parent="android:TextAppearance.Medium"> <item name="android:textColor">#000000</item> <item name="android:textStyle">bold</item> </style> </resources> - Theme: Theme definitions applied to entire Activity or application. Inherit from styles. Stored in XML files in
valuessubdirectories. - Array: Arrays of string values, integers, or resource references. Stored in XML files in
valuessubdirectories.<resources> <string-array name="planets_array"> <item>Mercury</item> <item>Venus</item> <item>Earth</item> <item>Mars</item> </string-array> </resources> - Color: Color definitions in hexadecimal or references to other colors. Stored in XML files in
valuessubdirectories (e.g.,values/colors.xml).<resources> <color name="colorPrimary">#008577</color> <color name="colorAccent">#03DAC5</color> </resources> - Dimension: Size definitions such as margins, paddings, font sizes, etc., with units (dp, sp, pt, px, mm, in). Stored in XML files in
valuessubdirectories (e.g.,values/dimens.xml).<resources> <dimen name="margin_small">8dp</dimen> <dimen name="font_size_medium">16sp</dimen> </resources> - Raw: Arbitrary files in raw format (audio, video, text files, etc.). Accessed via input streams. Stored in
rawsubdirectories. - XML: Arbitrary XML files that can be parsed in the app (e.g., configuration files). Stored in
xmlsubdirectories.
Access to resources is through the automatically generated R class. For example, to access a string resource app_name:
// Java
String appName = getResources().getString(R.string.app_name);
// Kotlin
val appName = resources.getString(R.string.app_name)
Using resources allows creating flexible and easily maintainable Android applications.