Android Platform Info

android:id="@+id/answer" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text=""

</LinearLayout>

The layout for this application is very straightforward. The overall layout is a vertical, linear layout with only four elements. A static TextView displays the title of the application O. An EditText collects the price of the meal for this Tip Calculator application ©. The EditText element has an attribute of type android:id, with a value of mealprice ©. When a UI element contains the android:id attribute, it permits us to manipulate this element from our code. We accomplish this by adding this element's id attribute to the R.java file as a unique member of the R class. This identifying value is used in the findViewByld method, shown in listing 2.2. If a UI element is static, such as the TextView O, and does not need to be set or read from our application code, the android:id attribute is not required.

A button named calculate © is added to the view. Note that this element also has an android:id attribute because we will want to capture click events.

A TextView named answer © is provided for displaying our total cost, including tip. Again, this element has an id because we will need to update it during runtime.

When we save the file main.xml, the file is processed by the ADT plug-in, compiling the resources and generating an updated R.java file. Try it for yourself. Modify one of the id values in the main.xml file, save the file, then open R.java to have a look at the constants generated there. Remember not to modify the R.java file directly, because all of your changes will be lost! If you conduct this experiment, be sure to change the values back as they are listed here to make sure the rest of the project will compile asis. Provided we have not introduced any syntactical errors into our main.xml file, our UI file is complete.

TIP Through the maturation of the still very young Android Development Tools, the plug-ins for Eclipse have offered increasingly useful resource editors for manipulating the layout xml files. This means that you do not need to rely on editing the xml files directly.

It is time to turn our attention to the file ChapterTwojava to implement the desired Tip Calculator functionality. ChapterTwo.java is shown in listing 2.2. Note that we omitted some imports for brevity. You can download the complete source code from the Manning website at http://manning.com/ableson.

0 0

Post a comment