CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View 197
Before I show you how to “wire up” your Galaxy object to your UI design in Java, so that your data
display TextView objects display your Galaxy object characteristics, I want to first show you how to
organize your Java code a little bit better, by creating your own custom methods in your MainActivity
class, to organize your Java code according to task. This is primarily so that your onCreate( ) method
does not turn into one huge Java code construct. Creating logical methods is called “modularizing”
your Java code, and makes it much more elegant, organized, and logical, as you will see during the
remainder of this chapter as you create three methods.
Creating Modular Code: createDefaultGalaxy( )
Let’s make our Java code more modular by putting the Galaxy class–related code into its own
method, called createDefaultGalaxy( ), so that all we have to call in our onCreate( ) method is a
single createDefaultGalaxy( ) method call. We will perform the same modularization in the creation
of the TextView UI objects later on, by using a method we will name createUiTextViews( ). We will
do the same thing for the transfer of the data field values from the Galaxy object to the TextView UI
widget objects, via a method named transferDataValues( ).
This modularization happens to be easy to do, which is why I decided to include it early on in this
book. Since we just had the chapter covering Java, let’s get some practice writing modular Java
code right away, so you can build on what you learned in the previous chapter. First, create the
empty method by typing the following code:
private void createDefaultGalaxy() { ... }
Figure 6-27. Adding the android:id parameter to the seven TextView UI elements which will hold the data values