Coding Your Application android application development for for dummies

123

Chapter 5: Coding Your Application

public class MainActivity extends Activity { Called when the activity is first created. Override public void onCreateBundle savedInstanceState { super.onCreatesavedInstanceState; setContentViewR.layout.main; } } You will be writing initialization code directly below the setContentView method shortly. Pay attention to the following line: super.onCreatesavedInstanceState; This line of code is required for the application to run. This calls to the base Activity class to perform setup work for the MainActivity class. If you do not include this line of code, you will receive a run-time exception. Be sure that you always include this method call to your onCreate method. Handling the bundle In the code snippet above, notice the following: Bundle savedInstanceState The bundle is a key value that maps between string keys and various parcel- able types. A bundle gives you, the developer, a way to pass information back and forth between screens different activities in what’s known as a bundle. You can place various types into a bundle and receive them on the target activity. I cover this later in Part III, when I walk you through building the Task Reminder application. Telling Android to display the UI By default, an activity has no idea what its UI is. It could be a simple form that allows the user to type information in to be saved, it could be a visual camera–based augmented virtual reality application such as Layar in the Android Market, or it could be a drawn-on-the-fly UI such as a 2D or 3D game. As a developer, it is your job to tell the activity which layout the a ctivity should load. 124 Part II: Building and Publishing Your First Android Application To get the UI to show up on the screen, you have to set the content view for the activity. That is done with the following line of code: setContentViewR.layout.main; R.layout.main is the main.xml file that is located in the reslayouts directory. This is the layout you defined in the previous chapter. Handling user input The Silent Mode Toggle application does not have a lot of user interaction; actually it has very little. The only user interaction your application will have is a single button. The user taps the button to toggle the silent mode on the phone, and then the user can tap the button again to turn off the silent mode. To respond to this tap event, you need to register what is known as an event listener. An event listener responds to an event in the Android system. You find various types of events in the Android system, but two of the most com- monly used events are going to be touch events also known as clicks and keyboard events. Keyboard events Keyboard events occur when a particular keyboard key has been pressed. Why would you want to know about this? Take the example of providing hot keys for your application. If the user presses Alt+E, you may want your view to toggle into Edit mode. Responding to keyboard events allows you to do this. I will not be using keyboard events in this book, but if you need to in future applications, you need to override the onKeyDown method, as shown here: Override public boolean onKeyDownint keyCode, KeyEvent event { TODO Auto-generated method stub return super.onKeyDownkeyCode, event; } Touch events Touch events occur when the user taps a widget on the screen. The Android platform recognizes each tap event as a click event. From here on, the terms tap, click, and touch are synonymous. Examples of widgets that can respond to touch events include but are not limited to: ✓ Button ✓ ImageButton ✓ EditText 125

Chapter 5: Coding Your Application