How to Create a Tip Calculator App in Android 6
Introducing GridLayout, EditText, SeekBar, Event Handling, NumberFormat, Customizing the App’s Theme and Defining App Functionality with Java
The Tip Calculator app (Fig. 3.1(a)) calculates and displays the tip and total for a restaurant bill amount. As you touch the numeric keypad to enter the bill amount’s digits, the app calculates and displays the tip and total bill amounts for the current tip percentage (15% by default). You specify a tip percentage from 0% to 30% by moving the SeekBar thumb—this updates the displayed tip percentage and recalculates the tip and total. All numeric values are displayed using locale-specific formatting. Figure 3.1(b) shows the app after the user enters the amount 56.32 and changes the tip percentage to 25%.
Fig. 3.1 | Entering the bill total and calculating the tip.
You’ll begin by test-driving the app. Then we’ll overview the technologies you’ll use to create the app. You’ll build the app’s GUI using Android Studio’s layout editor and the Component Tree window. Finally, we’ll present the complete Java code for the app and do a detailed code walkthrough.
Note Regarding the Keyboard in Our Screen Captures
The keypad in Fig. 3.1 may differ, based on your AVD’s or device’s Android version or whether you’ve installed and selected a custom keyboard on your device. We configured our AVD to display the dark keyboard for better contrast in our screen captures. To do so:
- Touch the home () icon on your AVD or device.
- On the home screen, touch the launcher () icon, then open the Settings app.
- In the Personal section, touch Language and Input.
- On an AVD, touch Android Keyboard (AOSP). On a device touch Google Keyboard—we assume you’re using the standard Android keyboard.
- Touch Appearance & layouts, then touch Theme.
- Touch Material Dark to change to the keyboard with the dark background.