A (175)

(Tuis.) #1

198 CHAPTER 6: Android Screen Design: Writing to the Display Using Activity and View


Then all you have to do is to cut and paste the five lines of Galaxy object code inside the curly
braces, like this:


private void createDefaultGalaxy() {
Galaxy milkyWay = new Galaxy("Milky Way", 511, 97);
milkyWay.setGalaxyColonies(37579231);
milkyWay.setGalaxyPopulation(1967387132);
milkyWay.setGalaxyFleets(237);
milkyWay.setGalaxyStarships(34769);
}


See, that was easy, as you can see in Figure 6-28. The createUiTextViews( ) method will be more
advanced, and the transferDataValues( ) method will be more advanced, and you’ll even learn how to
“nest” Java method calls!


Figure 6-28. Creating a private void createDefaultGalaxy( ) method to modularize our Galaxy object–related code


Now we’re ready to declare TextView objects for use, naming them while we’re at it so we can
instantiate them.


Updating the UI in Java: Using UI Objects


In order to have our Java program logic access or utilize our UI elements, we must instantiate (that
is, create objects for) each UI widget or UI layout container that we wish to modify or control using
Java program logic. There are two ways to do this, both of which we will take a look at in detail
during the remainder of this chapter. To declare a TextView object for use and give it a name, simply
call the TextView class and give it a name, using the following Java code, which looks a lot like your
variable declarations, but it declares an object:


TextView nameData;

Free download pdf