A Close-Up Look at an Android App
In this figure, the Package Explorer in Eclipse shows the structure of a newly created Android project. Each branch of the tree represents a file or a folder, and if you expand all branches of the tree, you see even more files and folders. Why so many files and folders in an Android project?
The src directory
The src directory contains the project’s Java source code. Files in this directory have names such as MainActivity.java, MyService.java, DatabaseHelper.java, and MoreStuff.java.
You can cram hundreds of Java files into a project’s src directory. But when you create a new project, Android typically creates only one file for you.
The res directory
A project’s res directory contains resources for use by the Android application. In the figure, you see that res has a bunch of subdirectories: four drawable directories, a layout directory, a menu directory, and three values directories.
The drawable subdirectories
The drawable directories contain images, shapes, and other elements.
Each drawable directory applies to certain screen resolutions. For example, in the name drawable-hdpi, the letters hdpi stand for high number of dots per inch. Files in the drawable-hdpi directory apply to devices whose resolutions are (roughly) between 180 and 280 dots per inch.
For more information about Android screen resolutions, visit the Android Developers website.
In the figure, the drawable-hdpi directory contains one file named ic_launcher.png. This file describes the image that appears on the app’s icon on the Android launcher screen.
The values subdirectory
An app’s res/values directory contains a file named strings.xml. (Refer to the figure.) This fisting shows the code in a simple strings.xml file.
<?xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">My First Android App</string> <string name="hello_world">Hello world!</string> <string name="menu_settings">Settings</string> </resources>
In the strings.xml file, you collect all the words, phrases, and sentences that the app’s user might see. You lump together phrases such as Hello world! and My First Android App so that someone can translate them all into different languages. With all those phrases collected in the strings.xml file, a translator doesn’t have to poke around to find phrases in the Java code.
(Poking around in the code in any real programming language can be dangerous because program code is intricate, and it can be brittle.)
The listing describes a “hello_world” string containing the characters Hello World! So in the app’s Java code, you refer to the words Hello world! by typing R.string.hello_world. To refer to the words Hello world! in another XML file (such as the one in the next listing), you type “@string/hello_world”. Either way, the text R.string.hello_world or the text “@string/hello_world” stands for the words Hello world! in the previous listing.
The use of strings.xml files helps with localization, which, in the tech world, is what you do to adapt an app to a culture’s local language and customs. To localize the app for French-speaking users, for example, you create an additional folder named values-fr. You add this folder to the tree shown in the figure.
Inside the values-fr folder, you create a second strings.xml file, and the new strings.xml file contains a line such as this one:
<string name="hello_world">Bonjour tout le monde!</string>
For Romanian, you create a values-ro directory, containing a strings.xml file with this line:
<string name="hello_world">Salut lume!</string>
When Android sees either R.string.hello_world or “@string/hello_world” in the code, Android determines the user’s country of origin and automatically displays the correct translation. This localization happens with no further effort on your part.
The layout subdirectory
The layout directory contains descriptions of the activities’ screens.
A minimal app’s res/layout directory contains an XML file describing an activity’s screen. (Refer to the activity_main.xml branch in the figure.) This listing shows the code in the simple activity_main.xml file.
<RelativeLayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" > <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_centerVertical="true" android:text="@string/hello_world" /> </RelativeLayout>
The code in this listing specifies that the layout of the app’s activity is a RelativeLayout (whatever that means) and, centered inside the RelativeLayout, you have a TextView. This TextView thingy is a little label containing the words Hello world!