android resources organizing and accessing

19
Resources Organizing & Accessing Ms. Truong Thi Ngoc Phuong Faculty of Information Technology HCMC University of Technology and Education

Upload: cong-thanh-nguyen

Post on 07-Jan-2017

853 views

Category:

Technology


2 download

TRANSCRIPT

Page 1: Android  Resources organizing and accessing

Resources Organizing & Accessing

Ms. Truong Thi Ngoc PhuongFaculty of Information Technology

HCMC University of Technology and Education

Page 2: Android  Resources organizing and accessing

Content Resource Overview Default Resources Alternative Resources Creating Resources Accessing Resources Create resources for varying screens Application Localization and String Resources

Page 3: Android  Resources organizing and accessing

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

Page 4: Android  Resources organizing and accessing

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

Page 5: Android  Resources organizing and accessing

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.

Page 6: Android  Resources organizing and accessing

Creating Resources Color Resources

Page 7: Android  Resources organizing and accessing

Creating Resources Dimension

Unit used: dp, sp, pt, px, mm, in

Page 8: Android  Resources organizing and accessing

Creating Resources Bool

Page 9: Android  Resources organizing and accessing

Creating Resources Integer

Page 10: Android  Resources organizing and accessing

Creating Resources String

Page 11: Android  Resources organizing and accessing

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

Page 12: Android  Resources organizing and accessing

Creating Resources Drawable Resources

Page 13: Android  Resources organizing and accessing

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

Page 14: Android  Resources organizing and accessing

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

Page 15: Android  Resources organizing and accessing

Creating Resources Menu ResourcesView link

Page 16: Android  Resources organizing and accessing

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

Page 17: Android  Resources organizing and accessing

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).

Page 18: Android  Resources organizing and accessing

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).

Page 19: Android  Resources organizing and accessing

Thank you!