O'Reilly 9781449388416 Datasheet Page 62

  • Download
  • Add to my manuals
  • Print
  • Page
    / 74
  • Table of contents
  • BOOKMARKS
  • Rated. / 5. Based on customer reviews
Page view 61
android:layout_span="2"/>
</TableRow>
<!-- End of all rows and widgets -->
</TableLayout>
TableLayout and TableRow
After examining main.xml, you can gather that the TableLayout and TableRow are
straightforward to use. You create the TableLayout once, then insert a TableRow. Now
you are free to insert any other widgets, such as TextView, EditView, and so on, inside
this TableRow.
Do look at the attributes, especially android:stretchColumns, android:layout_column,
and android:layout_span, which allow you to place widgets the same way you would
use a regular HTML table. I recommend that you follow the links to these attributes
and read up on how they work for a TableLayout.
Controlling input values
Controlling input values: Look at the EditText widget in the main.xml file at
. This
is the first text field for entering the “Total Amount” of the check. We want only num-
bers here. We can accept decimal numbers because real restaurant checks can be for
dollars and cents, and not just dollars. So we use the android:numeric attribute with a
value of decimal. This will allow whole values like 10 and decimal values like 10.12,
but will prevent any other type of entry.
This is a simple and concise way to control input values, thus saving us the trouble of
writing validation code in the Tipster.java file, and ensuring that the user does not enter
erroneous values. This XML-based constraints feature of Android is quite powerful and
useful. You should explore all possible attributes that go with a particular widget to
extract maximum benefits from this XML shorthand way of setting constraints. In a
future release, unless I have missed it completely in this release, I hope that Android
allows for entering ranges for the android:numeric attribute so that we can define what
range of numbers we wish to accept.
Since ranges are not currently available (to the best of my knowledge), you will see later
on that we do have to check for certain values like zero or empty values to ensure that
our tip calculation arithmetic does not fail.
Examining Tipster.java
Now we will look at the Tipster.java file which controls our application. This is the
main class that does the layout, the event handling, and the application logic.
The Android Eclipse plug-in creates the Tipster.java file in our project with the default
code shown in Example 1-7.
50 | Chapter 1:Getting Started
Page view 61
1 2 ... 57 58 59 60 61 62 63 64 65 66 67 ... 73 74

Comments to this Manuals

No comments