From Layout XML to View Objects

How do XML elements in activity_quiz.xml become View objects? The answer starts in the QuizActivity class.

When you created the GeoQuiz project, a subclass of Activity named QuizActivity was created for you. The class file for QuizActivity is in the app/java directory of your project. The java directory is where the Java code for your project lives.

In the Project tool window, reveal the contents of the app/java directory and then the contents of the com.bignerdranch.android.geoquiz package. Open the QuizActivity.java file and take a look at its contents (Listing 1.4).

Listing 1.4  Default class file for QuizActivity (QuizActivity.java)

p​a​c​k​a​g​e​ ​c​o​m​.​b​i​g​n​e​r​d​r​a​n​c​h​.​a​n​d​r​o​i​d​.​g​e​o​q​u​i​z​;​

i​m​p​o​r​t​ ​a​n​d​r​o​i​d​.​s​u​p​p​o​r​t​.​v​7​.​a​p​p​.​A​p​p​C​o​m​p​a​t​A​c​t​i​v​i​t​y​;​
i​m​p​o​r​t​ ​a​n​d​r​o​i​d​.​o​s​.​B​u​n​d​l​e​;​
i​m​p​o​r​t​ ​a​n​d​r​o​i​d​.​v​i​e​w​.​M​e​n​u​;​
i​m​p​o​r​t​ ​a​n​d​r​o​i​d​.​v​i​e​w​.​M​e​n​u​I​t​e​m​;​


p​u​b​l​i​c​ ​c​l​a​s​s​ ​Q​u​i​z​A​c​t​i​v​i​t​y​ ​e​x​t​e​n​d​s​ ​A​p​p​C​o​m​p​a​t​A​c​t​i​v​i​t​y​ ​{​

 ​ ​ ​ ​@​O​v​e​r​r​i​d​e​
 ​ ​ ​ ​p​r​o​t​e​c​t​e​d​ ​v​o​i​d​ ​o​n​C​r​e​a​t​e​(​B​u​n​d​l​e​ ​s​a​v​e​d​I​n​s​t​a​n​c​e​S​t​a​t​e​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​s​u​p​e​r​.​o​n​C​r​e​a​t​e​(​s​a​v​e​d​I​n​s​t​a​n​c​e​S​t​a​t​e​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​s​e​t​C​o​n​t​e​n​t​V​i​e​w​(​R​.​l​a​y​o​u​t​.​a​c​t​i​v​i​t​y​_​q​u​i​z​)​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​@​O​v​e​r​r​i​d​e​
 ​ ​ ​ ​p​u​b​l​i​c​ ​b​o​o​l​e​a​n​ ​o​n​C​r​e​a​t​e​O​p​t​i​o​n​s​M​e​n​u​(​M​e​n​u​ ​m​e​n​u​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​g​e​t​M​e​n​u​I​n​f​l​a​t​e​r​(​)​.​i​n​f​l​a​t​e​(​R​.​m​e​n​u​.​q​u​i​z​,​ ​m​e​n​u​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​t​r​u​e​;​
 ​ ​ ​ ​}​

 ​ ​ ​ ​@​O​v​e​r​r​i​d​e​
 ​ ​ ​ ​p​u​b​l​i​c​ ​b​o​o​l​e​a​n​ ​o​n​O​p​t​i​o​n​s​I​t​e​m​S​e​l​e​c​t​e​d​(​M​e​n​u​I​t​e​m​ ​i​t​e​m​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​i​n​t​ ​i​d​ ​=​ ​i​t​e​m​.​g​e​t​I​t​e​m​I​d​(​)​;​
 ​ ​ ​ ​ ​ ​ ​ ​i​f​ ​(​i​d​ ​=​=​ ​R​.​i​d​.​a​c​t​i​o​n​_​s​e​t​t​i​n​g​s​)​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​t​r​u​e​;​
 ​ ​ ​ ​ ​ ​ ​ ​}​
 ​ ​ ​ ​ ​ ​ ​ ​r​e​t​u​r​n​ ​s​u​p​e​r​.​o​n​O​p​t​i​o​n​s​I​t​e​m​S​e​l​e​c​t​e​d​(​i​t​e​m​)​;​
 ​ ​ ​ ​}​
}​

(Wondering what AppCompatActivity is? It is a subclass of Android’s Activity class that provides compatibility support for older versions of Android. You will learn much more about AppCompatActivity in Chapter 13.)

If you are not seeing all of the import statements, click the symbol to the left of the first import statement to reveal the others.

This file has three Activity methods: onCreate(Bundle), onCreateOptionsMenu(Menu), and onOptionsItemSelected(MenuItem).

Ignore onCreateOptionsMenu(Menu) and onOptionsItemSelected(MenuItem) for now. You will return to menus in detail in Chapter 13.

The onCreate(Bundle) method is called when an instance of the activity subclass is created. When an activity is created, it needs a user interface to manage. To get the activity its user interface, you call the following Activity method:

 ​ ​ ​ ​p​u​b​l​i​c​ ​v​o​i​d​ ​s​e​t​C​o​n​t​e​n​t​V​i​e​w​(​i​n​t​ ​l​a​y​o​u​t​R​e​s​I​D​)​

This method inflates a layout and puts it on screen. When a layout is inflated, each widget in the layout file is instantiated as defined by its attributes. You specify which layout to inflate by passing in the layout’s resource ID.

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 away 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

Changing the project view

Locate the dropdown at the top of the Project tool window and change from the Android project 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 that you are looking at here 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.

Listing 1.5  Current GeoQuiz resource IDs (R.java)

/​*​ ​A​U​T​O​-​G​E​N​E​R​A​T​E​D​ ​F​I​L​E​.​ ​ ​D​O​ ​N​O​T​ ​M​O​D​I​F​Y​.​
 ​*​
 ​*​ ​T​h​i​s​ ​c​l​a​s​s​ ​w​a​s​ ​a​u​t​o​m​a​t​i​c​a​l​l​y​ ​g​e​n​e​r​a​t​e​d​ ​b​y​ ​t​h​e​
 ​*​ ​a​a​p​t​ ​t​o​o​l​ ​f​r​o​m​ ​t​h​e​ ​r​e​s​o​u​r​c​e​ ​d​a​t​a​ ​i​t​ ​f​o​u​n​d​.​ ​ ​I​t​
 ​*​ ​s​h​o​u​l​d​ ​n​o​t​ ​b​e​ ​m​o​d​i​f​i​e​d​ ​b​y​ ​h​a​n​d​.​
 ​*​/​

p​a​c​k​a​g​e​ ​c​o​m​.​b​i​g​n​e​r​d​r​a​n​c​h​.​a​n​d​r​o​i​d​.​g​e​o​q​u​i​z​;​

p​u​b​l​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​R​ ​{​
 ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​a​n​i​m​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​
 ​ ​ ​ ​}​

 ​ ​ ​ ​.​.​.​

 ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​i​d​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​
 ​ ​ ​ ​}​
 ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​l​a​y​o​u​t​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​a​c​t​i​v​i​t​y​_​q​u​i​z​=​0​x​7​f​0​3​0​0​1​7​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​m​i​p​m​a​p​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​i​c​_​l​a​u​n​c​h​e​r​=​0​x​7​f​0​3​0​0​0​0​;​
 ​ ​ ​ ​}​
 ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​c​l​a​s​s​ ​s​t​r​i​n​g​ ​{​
 ​ ​ ​ ​ ​ ​ ​ ​.​.​.​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​a​p​p​_​n​a​m​e​=​0​x​7​f​0​a​0​0​1​0​;​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​c​o​r​r​e​c​t​_​t​o​a​s​t​=​0​x​7​f​0​a​0​0​1​1​;​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​f​a​l​s​e​_​b​u​t​t​o​n​=​0​x​7​f​0​a​0​0​1​2​;​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​i​n​c​o​r​r​e​c​t​_​t​o​a​s​t​=​0​x​7​f​0​a​0​0​1​3​;​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​q​u​e​s​t​i​o​n​_​t​e​x​t​=​0​x​7​f​0​a​0​0​1​4​;​
 ​ ​ ​ ​ ​ ​ ​ ​p​u​b​l​i​c​ ​s​t​a​t​i​c​ ​f​i​n​a​l​ ​i​n​t​ ​t​r​u​e​_​b​u​t​t​o​n​=​0​x​7​f​0​a​0​0​1​5​;​
 ​ ​ ​ ​}​
}​

The R.java file can be large and much of this file is omitted from Listing 1.5.

This is where the R.layout.activity_quiz comes from – it is an integer constant named activity_quiz within the layout inner class of R.

Your strings also have resource IDs. You have not yet referred to a string in code, but if you did, it would look like this:

 ​ ​ ​ ​s​e​t​T​i​t​l​e​(​R​.​s​t​r​i​n​g​.​a​p​p​_​n​a​m​e​)​;​

Android generated a resource ID for the entire layout and for each string, but it did not generate IDs for the individual widgets in activity_quiz.xml. Not every widget needs a resource ID. In this chapter, you will only interact with the two buttons in code, so only they need resource IDs.

Before generating the resource IDs, switch back to the Android project view. Throughout this book, the Android project view will be used – but feel free to use the Project version if you prefer.

To generate a resource ID for a widget, you include an android:id attribute in the widget’s definition. In activity_quiz.xml, add an android:id attribute to each button.

Listing 1.6  Adding IDs to Buttons (activity_quiz.xml)

<​L​i​n​e​a​r​L​a​y​o​u​t​ ​x​m​l​n​s​:​a​n​d​r​o​i​d​=​"​h​t​t​p​:​/​/​s​c​h​e​m​a​s​.​a​n​d​r​o​i​d​.​c​o​m​/​a​p​k​/​r​e​s​/​a​n​d​r​o​i​d​"​
.​.​.​ ​>​

 ​ ​<​T​e​x​t​V​i​e​w​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​w​i​d​t​h​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​h​e​i​g​h​t​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​p​a​d​d​i​n​g​=​"​2​4​d​p​"​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​t​e​x​t​=​"​@​s​t​r​i​n​g​/​q​u​e​s​t​i​o​n​_​t​e​x​t​"​ ​/​>​

 ​ ​<​L​i​n​e​a​r​L​a​y​o​u​t​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​w​i​d​t​h​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​h​e​i​g​h​t​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​a​n​d​r​o​i​d​:​o​r​i​e​n​t​a​t​i​o​n​=​"​h​o​r​i​z​o​n​t​a​l​"​>​

 ​ ​ ​ ​<​B​u​t​t​o​n​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​i​d​=​"​@​+​i​d​/​t​r​u​e​_​b​u​t​t​o​n​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​w​i​d​t​h​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​h​e​i​g​h​t​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​t​e​x​t​=​"​@​s​t​r​i​n​g​/​t​r​u​e​_​b​u​t​t​o​n​"​ ​/​>​

 ​ ​ ​ ​<​B​u​t​t​o​n​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​i​d​=​"​@​+​i​d​/​f​a​l​s​e​_​b​u​t​t​o​n​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​w​i​d​t​h​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​l​a​y​o​u​t​_​h​e​i​g​h​t​=​"​w​r​a​p​_​c​o​n​t​e​n​t​"​
 ​ ​ ​ ​ ​ ​a​n​d​r​o​i​d​:​t​e​x​t​=​"​@​s​t​r​i​n​g​/​f​a​l​s​e​_​b​u​t​t​o​n​"​ ​/​>​

 ​ ​<​/​L​i​n​e​a​r​L​a​y​o​u​t​>​

<​/​L​i​n​e​a​r​L​a​y​o​u​t​>​

Notice that there is a + sign in the values for android:id but not in the values for android:text. This is because you are creating the IDs and only referencing the strings.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.220.154.41