Android App Development - 03 Resources

Embed Size (px)

Citation preview

  1. 1. Resources
  2. 2. Resources To enable support for different configurations of the device, an application uses resources in the /res folder of the project. The folder contains default and alternative resources (for different configurations). The default resources and all the alternatives must be identified by the same name. The compiler creates a /gen folder that creates an enumeration with all the resources available to the application. The default resources must always be present because they are loaded at runtime when no other resource is present. Resources
  3. 3. Resources Animator: it contains .xml files with Property Animations Anim: it contains .xml files with View Animations Color: it contains .xml files with Color Selector Drawable: it can contain .png or .xml files Layout: it contains .xml files with layouts Menu: it contains .xml files with Options menus, Context menus and/or Sub menus Raw: it contains all other files to recover during runtime Values: it contains .xml files with values for differnt configurations. They are: strings, arrays, dimens, colors, styles Xml: it contains generic .xml files Types
  4. 4. Resources You need to create the /res subfolder that specify the name of the type of configuration that supports to provide alternative resources to support different configurations: - is the type of resource that contains as indicated above and identifies the supported configuration. Ex. res/layout-land contains the layout to be used when the phone is in landscape mode. You can specify multiple qualifiers in series. Alternative Resources
  5. 5. Resources The most commonly used types of qualifiers are listed in the same order in which they must be specified in the name of the /res subfolder: Language (Ex. en, fr, it,...): it specifies in which language resources are available within the folder SmallestWidth (swdp Es. sw320dp, sw720dp,...): the resources of these folders are available only if the terminal has Min(dp_height, dp_width) >= n Screen Size (small, normal, large, xlarge): Qualifiers - Typologies
  6. 6. Resources ScreenOrientation (port, land) Screen Pixel Density (ldpi, mdpi, hdpi, xhdpi, xxhdpi, xxxhdpi, tvdpi) ldpi: 120dp mdpi: 160dp hdpi: 240dp xhdpi: 320dp xxhdpi: 480dpi xxxhdpi: 640dp Platform Version (Es. V12, v15, v19, ...): the logic is top bottom. Ex. A device with V19 verifies the presence of the resource in v19, then in v18, and so on until it finds one. Qualifiers - Tipologie
  7. 7. Resources The multiple qualifiers must be specified in the order indicated before: Es. res/drawable-port-hdpi/ The folders defined with these rules can not contain subfolders The folder names are case-insensitive It is allowed only one value for each type of qualifier. Es. res/drawable/it-en INCORRECT If you want to provide the same resource configuration for it and en, you have to create 2 folders and enter the same resource in both res/drawable-it CORRECT res/drawable-en CORRECT Qualifiers - Rules
  8. 8. Resources When you insert a new resource in the project, the aapt automatically generates the R class that contains all the IDs of resources on the basis of fundamental subdirectory in which you can retrieve them at runtime: R.anim. R.array. R.bool. R.color. R.dimen. R.drawable. R.integer. R.layout. R.menu. R.raw. R.string. R.style. Resource access at runtime
  9. 9. Resources The R class is generated for each project and it is created in the package defined in the manifest: [.]R.. is optional if the class is imported into the R class is the same from which you want to retrieve a resource. In this way it is possible to access resources outside projects and imported to the system resources of Android for which = android Then the developer may require a system resource with the expression: android.R.. N.B.: system resources at runtime are taken from the firmware of the terminal, so the same ID can refer to different resources in terminals with different firmware. Resource access at runtime
  10. 10. Resources To access resources from an xml file using the following syntax: @[:]/ is optional if you want to use a resource package of the project. Pattern: during the creation of the layout you should always include references to external resources instead of entering the value in the layout. Es. android:text="@string/hello" CORRECT android:text="Hello!" CORRECT BUT NOT RECOMMENDED Resource access from an xml file
  11. 11. Resources Layout
  12. 12. Resources text_string String
  13. 13. Resources View Animation
  14. 14. Resources Property Animation
  15. 15. Resources Menu
  16. 16. Resources Style
  17. 17. Resources Drawable Selector
  18. 18. Resources Shape
  19. 19. Resources [true | false]hex_colordimensionintegerintegerresource Other Resources