3.5. Adding Functionality to the App

Class MainActivity (Figs. 3.93.16) implements the Tip Calculator app’s functionality. It calculates the 15% and custom percentage tips and total bill amounts, and displays them in locale-specific currency format. To view the file, open src/com.deitel/tipcalculator and double clck MainActivity.java. You’ll need to enter most of the code in Figs. 3.93.16.


 1   // MainActivity.java
 2   // Calculates bills using 15% and custom percentage tips.
 3   package com.deitel.tipcalculator;
 4
 5   import java.text.NumberFormat; // for currency formatting
 6
 7   import android.app.Activity; // base class for activities
 8   import android.os.Bundle; // for saving state information
 9   import android.text.Editable; // for EditText event handling
10   import android.text.TextWatcher; // EditText listener
11   import android.widget.EditText; // for bill amount input
12   import android.widget.SeekBar; // for changing custom tip percentage
13   import android.widget.SeekBar.OnSeekBarChangeListener; // SeekBar listener
14   import android.widget.TextView; // for displaying text
15


Fig. 3.9 | MainActivity’s package and import statements.


16   // MainActivity class for the Tip Calculator app
17   public class MainActivity extends Activity
18   {


Fig. 3.10 | Class MainActivity is a subclass of Activity.


19     // currency and percent formatters
20     private static final NumberFormat currencyFormat =
21        NumberFormat.getCurrencyInstance();
22     private static final NumberFormat percentFormat =
23        NumberFormat.getPercentInstance();
24
25     private double billAmount = 0.0; // bill amount entered by the user
26     private double customPercent = 0.18; // initial custom tip percentage
27     private TextView amountDisplayTextView; // shows formatted bill amount
28     private TextView percentCustomTextView; // shows custom tip percentage
29     private TextView tip15TextView; // shows 15% tip
30     private TextView total15TextView; // shows total with 15% tip
31     private TextView tipCustomTextView; // shows custom tip amount
32     private TextView totalCustomTextView; // shows total with custom tip
33


Fig. 3.11 | MainActivity class’s instance variables.


34     // called when the activity is first created
35     @Override                                          
36     protected void onCreate(Bundle savedInstanceState)
37     {
38        super.onCreate(savedInstanceState); // call superclass's version
39        setContentView(R.layout.activity_main); // inflate the GUI
40
41        // get references to the TextViews
42        // that MainActivity interacts with programmatically
43        amountDisplayTextView =                                 
44           (TextView) findViewById(R.id.amountDisplayTextView );
45        percentCustomTextView =
46           (TextView) findViewById(R.id.percentCustomTextView );
47        tip15TextView = (TextView) findViewById(R.id.tip15TextView);
48        total15TextView = (TextView) findViewById(R.id.total15TextView);
49        tipCustomTextView = (TextView) findViewById(R.id.tipCustomTextView);
50        totalCustomTextView =
51           (TextView) findViewById(R.id.totalCustomTextView);
52
53        // update GUI based on billAmount and customPercent
54        amountDisplayTextView.setText(
55           currencyFormat.format(billAmount));
56        updateStandard(); // update the 15% tip TextViews
57        updateCustom(); // update the custom tip TextViews
58
59        // set amountEditText's TextWatcher
60        EditText amountEditText =
61           (EditText) findViewById(R.id.amountEditText);
62        amountEditText.addTextChangedListener(amountEditTextWatcher);
63
64        // set customTipSeekBar's OnSeekBarChangeListener
65        SeekBar customTipSeekBar =
66           (SeekBar) findViewById(R.id.customTipSeekBar);
67        customTipSeekBar.setOnSeekBarChangeListener(customSeekBarListener);
68     } // end method onCreate
69


Fig. 3.12 | Overriding Activity method onCreate.


70     // updates 15% tip TextViews
71     private void updateStandard()
72     {
73        // calculate 15% tip and total
74        double fifteenPercentTip = billAmount * 0.15;
75        double fifteenPercentTotal = billAmount + fifteenPercentTip;
76
77        // display 15% tip and total formatted as currency
78        tip15TextView.setText(currencyFormat.format(fifteenPercentTip));
79        total15TextView.setText(currencyFormat.format(fifteenPercentTotal));
80     } // end method updateStandard
81


Fig. 3.13 | Method updateStandard calculates and displays the 15% tip and total.


82     // updates the custom tip and total TextViews
83     private void updateCustom()
84     {
85        // show customPercent in percentCustomTextView formatted as %
86        percentCustomTextView.setText(percentFormat.format(customPercent));
87
88        // calculate the custom tip and total
89        double customTip = billAmount * customPercent;
90        double customTotal = billAmount + customTip;
91
92        // display custom tip and total formatted as currency
93        tipCustomTextView.setText(currencyFormat.format(customTip));
94        totalCustomTextView.setText(currencyFormat.format(customTotal));
95     } // end method updateCustom
96


Fig. 3.14 | Method updateCustom calculates and displays the custom tip and total.


97     // called when the user changes the position of SeekBar
98     private OnSeekBarChangeListener customSeekBarListener =
99        new OnSeekBarChangeListener()
100       {
101          // update customPercent, then call updateCustom
102          @Override                                                   
103          public void onProgressChanged(SeekBar seekBar, int progress,
104             boolean fromUser)                                        
105          {
106             // sets customPercent to position of the SeekBar's thumb
107             customPercent = progress / 100.0 ;
108             updateCustom(); // update the custom tip TextViews
109          } // end method onProgressChanged
110
111          @Override
112          public void onStartTrackingTouch(SeekBar seekBar)
113          {
114          } // end method onStartTrackingTouch
115
116          @Override
117          public void onStopTrackingTouch(SeekBar seekBar)
118          {
119          } // end method onStopTrackingTouch
120       }; // end OnSeekBarChangeListener
121


Fig. 3.15 | Anonymous inner class that implements interface OnSeekBarChangeListener to respond to the events of the customSeekBar.


122     // event-handling object that responds to amountEditText's events
123     private TextWatcher amountEditTextWatcher = new TextWatcher()
124     {
125        // called when the user enters a number
126        @Override                                           
127        public void onTextChanged(CharSequence s, int start,
128           int before, int count)                           
129        {
130           // convert amountEditText's text to a double
131           try
132           {
133              billAmount = Double.parseDouble(s.toString()) / 100.0 ;
134           } // end try
135           catch (NumberFormatException e)
136           {
137              billAmount = 0.0; // default if an exception occurs
138           } // end catch
139
140           // display currency formatted bill amount
141           amountDisplayTextView.setText(currencyFormat.format(billAmount));
142           updateStandard(); // update the 15% tip TextViews
143           updateCustom(); // update the custom tip TextViews
144        } // end method onTextChanged
145
146        @Override
147        public void afterTextChanged(Editable s)
148        {
149        } // end method afterTextChanged
150
151        @Override
152        public void beforeTextChanged(CharSequence s, int start, int count,
153           int after)
154        {
155        } // end method beforeTextChanged
156     }; // end amountEditTextWatcher
157  } // end class MainActivity


Fig. 3.16 | Anonymous inner class that implements interface TextWatcher to respond to the events of the amountEditText.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset