Android Tips, Techniques and Theory
PART 1: Creating Content with XML with Android Studio (No programming required)
With Simple Recipes
Go Back To TOC
XML
XML (eXtensible Markup Language) defines a set of rules for encoding a document that is both human and machine readable. It is used to define many resources files within Android in which many are created automatically when a new Android project is created in specific directories. Some resources; however, have to be created manually.
Manifests directory:
- Manifest file (e.g., AndroidManifest.xml)
res/drawable directory:
- Custom selectors (e.g., customButtonSelector.xml)
- Shape drawables (e.g., myShapeDrawable.xml)
NOTE: This folder also holds custom drawable images.
res/layout directory:
- UI Activity layouts (e.g., activity_main.xml)
res/menu directory:
- Menus and FABs (e.g., menu_main.xml)
res/values directory:
- String arrays (e.g., myData.xml)
- Color schemes (e.g., colors.xml)
- Dimensions and predefined values (e.g., dimens.xml)
- String constants (e.g., strings.xml)
- Styles and themes (e.g., styles.xml)
- Animations (e.g., myAnimation.xml)
- Raw Files (e.g., myGameFile.xml, myTextFile.xml, myHelpFile.xml)
res/xml directory:
res/raw directory:
NOTES:
- Strings, Color, Dimensions and Styles, Drawables, Layout resources and directories are created automatically.
- Resource files may contain letters, numbers underscores and must be lowercase.
- When resource files are compiled, their name will determine their variable name. For example, myImage.png in the drawable folder will be referenced or named myImage without the “.png” extension. (e.g., @drawable/myImage).
- When a resource file is added or updated in a resource directory, the R.java file (Discussed in Part 2) is recompiled to reflect the changes.
id attribute
Each component (view) that needs to be referenced by a Java file is required to have an id:
- @+id/componentID—create a new id as an integer in the R.java that is auto-generated file.
FYI for ultra-geeks only: This integer is a 4 byte hexadecimal constant (e.g., public static final int action_setting=0x7f050001)
You may also see the syntax without a plus sign (+) that can be used when referring to an existing id with the same name in RelativeLayout container when referring another component for its id.
TIPS: While the Layout Editor (See next topic) automatically creates ids for each component, it is best practice to give those components more descriptive names with the same naming convention for each (e.g., playGameButtonId). Also, it is OK to use the first syntax for everything.