ANDROID Resources
ANDROID
Android resources are external files and static files used by application code such as images, layouts, strings, styles, etc.
Android Resources
➔ Android resources are kept in a separate location in the project for better maintenance and localization purposes.
➔ These resources sometimes help the device adapt to different configurations, screen sizes, and languages.
Organizing resource folders.
All resources are kept in specially named subdirectories inside the project's res folder.
➔ anim/ : XML file that defines the property or animation.
➔ color/ : XML file that defines a list of colors used in the project.
➔ drawable/ : Bitmap files (PNG, JPG, GIF) or XML files can be used as drawable subtypes like shapes.
➔ layout/ : XML file that defines the user interface layout and other UI elements.
➔ mipmap/ : Directory of different resolution versions of launcher icons to support different device screen densities.
➔ values/ : XML file for storing simple values such as strings, integers, dimensions, colors, styles, and themes.
- colors.xml is an XML file used to store colors for resources ie text color, background color etc.
- dimens.xml is an XML file used to store dimensions for resources ie element width, height etc
- strings.xml is an XML file used to store colors for resources ie any resource name.
- styles.xml is an XML file used to store style for resources ie text color, button style etc..
colors.xml resource file contains colors name value pairs.
Syntax
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="colorPrimary">#22c948</color>
<color name="colorPrimaryDark">#22c948</color>
<color name="colorAccent">#FF5070</color>
<color name="mycolor1">#111111</color>
<color name="mycolor2">#FFFFFF</color>
<color name="mycolor3">#108090</color>
</resources>
Syntax
<resources>
<dimen name="dimension1">16dp</dimen>
<dimen name="dimension2">24dp</dimen>
<dimen name="dimension3">8dp</dimen>
<dimen name="dimension4">36dp</dimen>
</resources>
Syntax
<resources>
<string name="app_name">MyApp</string>
<string name="button1_name">Clear</string>
<string name="button2_name">Submit</string>
<string name="action_settings">Settings</string>
<string name="gender_male">Male</string>
<string name="gender_female">Female</string>
</resources>
Syntax
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="MyTextStyle1" parent="TextAppearance.AppCompat.Small">
<item name="android:textColor">#00FF00</item>
<item name="android:textStyle">bold</item>
</style>
<style name="MyButtonStyle" parent="android:Widget.Button">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:background">@android:color/darker_gray</item>
<item name="android:padding">12dp</item>
</style>
</resources>
How to use styles in other resources:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
style="@style/MyTextStyle1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello, This is MyTextStyle1" />
<Button
style="@style/MyButtonStyle"
android:text="My Button" />
</LinearLayout>