Listening to Button clicks
Next we look at the 'Calculate' and Reset buttons. When the
user clicks these buttons, we use the static interface OnClickListener()
which will let us know when a button is clicked.
As we did with text fields, just one listener is created
and within it we detect which button was clicked. Depending on
the button clicked, the calculate() and reset() methods are
called.
Code snippet 6 shows how the click listener is added to the buttons.
Code snippet 7 shows how to detect which button is clicked by checking for the ID of the View that receives the click event.
Resetting the application
When the user clicks the Reset button, the text fields should be cleared, the default 15% radio butto should be seleced and any results calculated should be cleared.
Code snippet 8 shows the reset() method.
Validating the input to calculate the tip
As I said before, we are limiting what type of values the user can enter in the text fields. However, the user could still enter a value of zero in the Total Amount, No. of People and Other Tip Percentage text fields, thus causing error conditions like divide by zero in our tip calculations.
If the user enters zero then we must show an alert popup asking the
user to enter non-zero values. We handle this with a method
called showErrorAlert(String errorMessage, final int fieldId), but more about it later.
First, look at code snippet 9 which shows
the calculate() method. Notice how the values entered by the user are parsed as Double values.
Checking for zero values
Notice line 11 and 16 where we check for zero values. If the user enters zero, then we show an alert popup to warn the user. Next look at line 37, where the Other Tip Percentage text field is enabled because the user selected the Other radio button. Here too, we must check for the tip percentage being zero.
State of radio buttons
When the application loads, the 15% radio button is selected by default. If the user changes the selection, then
we saw in code snippet 3, in the OnCheckedChangeListener, that we assign the ID of the selected radio button
to the member variable radioCheckedId.
But if the user accepts the default selection, then the radioCheckedId will have the default value of -1.
In short, we will never know which radio button was selected. Offcourse, we know which one is selected by default and could
have coded the logic slightly differently, to assume 15% if radioCheckedId has the value -1. But if you refer the
API, you will see that we can call the method getCheckedRadioButtonId()
on the RadioGroup and not on individual radio buttons. This is because the OnCheckedChangeListener readily
provides us with the ID of the radio button selected.
Showing the results
Calculating the tip is simple. If there are no validation errors, the boolean flag isError will be false.
Look at lines 49 to 51 in code snippet 9 for the simple tip calculations. Next, the calculated values are set to the TextView
widgets from line 53 to 55.
Showing the alerts
Android provides the AlertDialog class to show alert popups.
This lets us show a dialog with upto three buttons and a message.
Code snippet 10 shows the showErrorAlert method which uses this AlertDialog to show the error messages.
Notice that we pass two arguments to this method - String errorMessage and int fieldId. The first argument is the
error message we want to show to the user. The fieldId is the ID of the field which caused the error condition. After the user dismissed the alert dialog,
this fieldID will allow us to request the focus on that field, so the user knows which field has the error.
Conclusion
Developing for the Android OS is not so different than developing for any other UI toolkit like Microsoft Windows, X Windows, Java Swing, Adobe Flex, etc. Offcourse Android has its differences and overall a very good design. The XML layout paradigm is quite cool and useful to build complex UIs using simple XML. The event handling model is simple, feature rich and intuitive to use in code.
I am enjoying delving into the Android OS and learning new things every time I dive in. I look forward to being able to deploy an application on a real Google Phone :)
Comments and Feedback
Please visit my blog to post your comments.
Resources
In this tipster.zip file, you will find the necessary Eclipse project and the source files. Download the zip file and extract it to a local folder. From Eclipse, simply use the File-->Import menu option to import the project.
Happy coding! :)
[Note: Please refer to the enhancement after you download the project zip file]