Sobes.tech
Junior

Τι είναι οι πόροι στο Android και ποια δεδομένα μπορούν να αποθηκευτούν σε αυτούς;

sobes.tech AI

Απάντηση από AI

Resources in Android are external data used by the application but not included directly in the code. They are separated from the app 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 app (supporting different languages).
  • Adapting to different screen sizes and resolutions.
  • Separating design from logic.
  • Simplifying interface updates.

The types of data that can be stored in resources include:

  • Drawable: Various graphic elements such as images (PNG, JPEG, GIF), XML shape definitions, switches, etc. Stored in drawable subfolders.
    <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 of an Activity, Fragment, or View. Stored in layout subfolders.
    <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 in mipmap subfolders (e.g., mipmap-hdpi, mipmap-xhdpi).
  • String: String values used in the app. Easily localized. Stored in XML files in values subfolders (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 values subfolders (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: Definitions of themes applied to the entire Activity or application. Inherit from styles. Stored in XML files in values subfolders.
  • Array: Arrays of string values, integers, or resource references. Stored in XML files in values subfolders.
    <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 format or references to other colors. Stored in XML files in values subfolders (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 values subfolders (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 raw subfolders.
  • XML: Arbitrary XML files that can be parsed in the app (e.g., configuration files). Stored in xml subfolders.

Access to resources is done through the automatically generated class R. For example, to access the 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.