Lines 20–32 of Fig. 3.11 declare class MainActivity
’s variables. The NumberFormat
objects (lines 20–23) are used to format currency values and percentages, respectively. NumberFormat static
method getCurrencyInstance
returns a NumberFormat
object that formats values as currency using the device’s default locale. Similarly, static
method getPercentInstance
formats values as percentages using the device’s default locale.
The bill amount entered by the user into amountEditText
will be read and stored as a double
in billAmount
(line 25). The custom tip percentage (an integer in the range 0–30) that the user sets by moving the Seekbar
thumb will be multiplied by 0.01 to create a double
for use in calculations, then stored in customPercent
(line 26). For example, if you select 25
with the SeekBar
, customPercent
will store 0.25
, so the app will multiply the bill amount by 0.25
to calculate the 25% tip.
Line 27 declares the TextView
that displays the currency-formatted bill amount. Line 28 declares the TextView
that displays the custom tip percentage based on the SeekBar
thumb’s position (see the 18% in Fig. 3.1(a)). The variables in line 29–32 will refer to the TextView
s in which the app displays the calculated tips and totals.