Android Apps for Absolute Beginners

Page 221

212

CHAPTER 9: Adding Interactivity: Handling UI Events

up navigation arrow when they are on (have focus on) the first UI button (contextButton from our prior example). To control advancement of focus from the contextButton to the secondButton button, we add this: android:nextFocusDown="@+id/secondButton" Now we have defined all of the focus movements that can happen for the contextButton, and we are ready to define the focus movements for the next two buttons. This will be a very similar process. In fact, you can simply cut and paste the two lines of code that you wrote for the contextButton tag and change the ID attributes after you paste them into the two new Button tags. For the second Button tag, we will add in another two android:nextFocus attributes. This time, these point to the buttons immediately above and below the second button, so this one is the easiest. The code looks as follows: android:nextFocusUp=”@+id/contextButton” android:nextFocusDown=”@+id/thirdButton”

For the third Button tag, we will add in another two android:nextFocus attributes, which finally point to the buttons immediately above and back up to the top button in our loop of buttons, as follows: android:nextFocusUp="@+id/secondButton" android:nextFocusDown="@+id/contextButton"

The first attribute is pretty straightforward, as the secondButton button is above our third button. For the nextFocusDown attribute, since there is no button underneath the third button, we actually want the focus to wrap, or loop back, to our first contextButton button, so that is the ID we use in the android:nextFocusDown attribute that we add to the final Button tag. NOTE: There are nextFocusLeft and nextFocusRight attributes available (one for each arrow key) if you are using a horizontal LinearLayout tag attribute, for instance. Here are the three blocks of nextFocus attributes that we have added to our three buttons so that you can check your work (see Figure 9–19): <Button android:text="Long-Click Here to Access ContextMenu" android:id="@+id/contextButton" android:nextFocusUp="@+id/thirdButton" android:nextFocusDown="@+id/secondButton" android:layout_gravity="center" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <Button android:text="Second Button" android:id="@+id/secondButton" android:nextFocusUp="@+id/contextButton"

www.it-ebooks.info


Issuu converts static files into: digital portfolios, online yearbooks, online catalogs, digital photo albums and more. Sign up and create your flipbook.