Android Programming The Big Nerd Ranch Guide, 3rd Edition

(Brent) #1

Chapter 1  Your First Android Application


18

Resources and resource IDs


A layout is a resource. A resource is a piece of your application that is not code – things like image
files, audio files, and XML files.


Resources for your project live in a subdirectory of the app/res directory. In the project tool window,
you can see that activity_quiz.xml lives in res/layout/. Your strings file, which contains string
resources, lives in res/values/.


To access a resource in code, you use its resource ID. The resource ID for your layout is
R.layout.activity_quiz.


To see the current resource IDs for GeoQuiz, you must first change your project view. By default,
Android Studio uses the Android project view (Figure 1.13). This view hides the true directory structure
of your Android project so that you can focus on the files and folders that you need most often.


Figure 1.13  Changing the project view


Locate the dropdown at the top of the project tool window and change from the Android view to the
Project view. The Project view will show you the files and folders in your project as they actually are.


To see the resources for GeoQuiz, reveal the contents of the app/build/generated/source/r/debug
directory. In this directory, find your project’s package name and open R.java within that package.
Because this file is generated by the Android build process, you should not change it, as you are subtly
warned at the top of the file.


After making a change to your resources, you may not see this file instantly update. Android Studio
maintains a hidden R.java that your code builds against. The R.java file in Listing 1.5 is the one that
is generated for your app just before it is installed on a device or emulator. You will see this file update
when you run your app.

Free download pdf