Date post: | 07-Jan-2017 |
Category: |
Technology |
Upload: | cong-thanh-nguyen |
View: | 853 times |
Download: | 2 times |
Resources Organizing & Accessing
Ms. Truong Thi Ngoc PhuongFaculty of Information Technology
HCMC University of Technology and Education
Content Resource Overview Default Resources Alternative Resources Creating Resources Accessing Resources Create resources for varying screens Application Localization and String Resources
Resource Overview Resource is an important part in Android apps. Stored in subfolders of /res There are 8 types of resources:
anim/: contains xml files that define property animations. From code, we can access by R.anim
color/: contains xmls files that define a state list of colors. From code, we can access by R.color
drawable/ contains image files or xml files that can compiled into bitmaps, state lists, shapes, animation drawables. From code, we can access by R.drawable
layout/ contains xml files that define user interfaces. From code, we can access by R.layout
Resource Overview (cont) menu/ contains xml files that define app menus such as Option
Menu, Context Menu or Sub Menu. From code, we can access by R.menu
raw/ contains arbitrary files in their raw form. You need to call Resources.openRawResource() with resource ID, which is R.raw.filename to open such raw files
xml/ contains arbitrary xml files that can be read at runtime by calling Resources().getXML()
values/ contains xml files storing simple values such as string, integer, color. There are some files in this folder: arrays.xml for resource arrays, and accessed from R.array Integers.xml for resource integer and accessed from R.integer bools.xml for resource boolean and accessed from R.bool colors.xml for color values and accessed from R.color dimens.xml for dimen values and accessed from R.dimen string.xml for string values and accessed from R.string styles.xml for styles and accessed from R.style
Resource Overview (cont) For any type of resources, you can specify
default and multiple alternative resources: Default resource: should be used regardless of
the device configuration or when there are no alternative resources that match the current configuration.
Alternative resources: should be used with a specific configuration.
Creating Resources Color Resources
Creating Resources Dimension
Unit used: dp, sp, pt, px, mm, in
Creating Resources Bool
Creating Resources Integer
Creating Resources String
Create Resources (Strings) In res/values
strings.xml Application wide available strings Promotes good software engineering UI components made in the UI editor should
have text defined in strings.xml
Strings are just one kind of ‘Value’, there are many others
Creating Resources Drawable Resources
Create Resources (Drawables) A general concept for a graphic that can be
drawn to the screen. Store in folder beginning with drawableXXX Can be bitmap files or xml bitmap files
Creating Resources Layout Resources
Eclipse has a great UI creator Generates the XML for you
Composed of View objects Can be specified for portrait and landscape mode
Use same file name, so can make completely different UIs for the orientations without modifying any code
Creating Resources Menu ResourcesView link
Manifest File Contains characteristics about your application When have more than one Activity in app, NEED to
specify it in manifest file Go to graphical view of the manifest file Add an Activity in the bottom right Browse for the name of the activity
Need to specify Services and other components too Also important to define permissions and external
libraries, like Google Maps API
Acessing Resources In Code
[<package_name>.]R.<resource_type>.<resource_name> <package_name> is the name of the package in which the resource is located
(not required when referencing resources from your own package). <resource_type> is the R subclass for the resource type. <resource_name> is either the resource filename without the extension or the
android:name attribute value in the XML element (for simple values).
Acessing Resources From XML
@[<package_name>:]<resource_type>/<resource_name><package_name> is the name of the package in which the resource is located (not required when referencing resources from the same package)<resource_type> is the R subclass for the resource type<resource_name> is either the resource filename without the extension or the android:name attribute value in the XML element (for simple values).
Thank you!