A (175)

(Tuis.) #1

380 CHAPTER 10: Android Animation: Making Your UI Designs Move


Next, add a line of code (three actually, one per ImageButton object) under your
transferDataValues( ) method call, that instantiates each ImageButton object and references it to
a galaxyOne, galaxyTwo, and galaxyThree android:id value, which you assigned earlier when you
were designing your XML UI layout definition. The Java code is written with the following three lines
of Java code, which is shown error-free in Figure 10-20:


imageButtonOne = (ImageButton)findViewById(R.id.galaxyOne);
imageButtonTwo = (ImageButton)findViewById(R.id.galaxyTwo);
imageButtonThree = (ImageButton)findViewById(R.id.galaxyThree);


Figure 10-20. Instantiate three ImageButton objects inside the onCreate( ) method using the findViewById( ) method


Now we have created all of the objects that we need, so let’s get even more complicated and add
event handler logic to each of the ImageButton objects, so something will happen when our user
clicks on any of these buttons!


Making Your UI Interactive: Add Event Handling


One of the reasons I covered event listeners and event handling early on in this book, during
Chapter 7, was so that I had the option, when I wanted it, to show you how to create interactive
aspects of an application. I want to do this here by making the ImageButtons control the animation
I’m creating for the different galaxy information screens. I have been putting off finishing this galaxy
information screen UI design for a couple of chapters now. Therefore, in this chapter, I’m going to show
you how to add event handling to the ImageButton widgets and how to add Java logic inside of these
event handler structures. The Java logic inside the event handlers will change depending upon which
of the different types of animation you are using. We will also be completing the Java programming
logic which will make your other galaxy information screen user interface designs interactive.

Free download pdf