Answered step by step
Verified Expert Solution
Question
1 Approved Answer
package com.murach.tipcalculator; import java.text.NumberFormat; I need help completing this don't know where to start Java import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo;
package com.murach.tipcalculator; import java.text.NumberFormat; I need help completing this don't know where to start Java import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class TipCalculatorActivity extends Activity implements OnEditorActionListener, OnClickListener { // define variables for the widgets private EditText billAmountEditText; private TextView percentTextView; private Button percentUpButton; private Button percentDownButton; private TextView tipTextView; private TextView totalTextView; // define the SharedPreferences object private SharedPreferences savedValues; // define instance variables that should be saved private String billAmountString = ""; private float tipPercent = .15f; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tip_calculator); // get references to the widgets billAmountEditText = (EditText) findViewById(R.id.billAmountEditText); percentTextView = (TextView) findViewById(R.id.percentTextView); percentUpButton = (Button) findViewById(R.id.percentUpButton); percentDownButton = (Button) findViewById(R.id.percentDownButton); tipTextView = (TextView) findViewById(R.id.tipTextView); totalTextView = (TextView) findViewById(R.id.totalTextView); // set the listeners billAmountEditText.setOnEditorActionListener(this); percentUpButton.setOnClickListener(this); percentDownButton.setOnClickListener(this); // get SharedPreferences object savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE); } @Override public void onPause() { // save the instance variables Editor editor = savedValues.edit(); editor.putString("billAmountString", billAmountString); editor.putFloat("tipPercent", tipPercent); editor.commit(); super.onPause(); } @Override public void onResume() { super.onResume(); // get the instance variables billAmountString = savedValues.getString("billAmountString", ""); tipPercent = savedValues.getFloat("tipPercent", 0.15f); // set the bill amount on its widget billAmountEditText.setText(billAmountString); // calculate and display calculateAndDisplay(); } public void calculateAndDisplay() { // get the bill amount billAmountString = billAmountEditText.getText().toString(); float billAmount = Float.parseFloat(billAmountString); // calculate tip and total float tipAmount = billAmount * tipPercent; float totalAmount = billAmount + tipAmount; // display the other results with formatting NumberFormat currency = NumberFormat.getCurrencyInstance(); tipTextView.setText(currency.format(tipAmount)); totalTextView.setText(currency.format(totalAmount)); NumberFormat percent = NumberFormat.getPercentInstance(); percentTextView.setText(percent.format(tipPercent)); } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) { calculateAndDisplay(); } return false; } @Override public void onClick(View v) { switch (v.getId()) { case R.id.percentDownButton: tipPercent = tipPercent - .01f; calculateAndDisplay(); break; case R.id.percentUpButton: tipPercent = tipPercent + .01f; calculateAndDisplay(); break; } } }
XMLNS
manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.murach.tipcalculator" android:versionCode="1" android:versionName="1.0" > application android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > activity android:name=".TipCalculatorActivity" android:label="@string/title_activity_tip_calculator" > intent-filter> action android:name="android.intent.action.MAIN" /> category android:name="android.intent.category.LAUNCHER" /> intent-filter> activity> application> manifest>Chapter 4 How to test and debug an Android app 115 Exercise 4-1 Test and debug the Tip Calculator app This exercise guides you through the process of using Andr oid Studio to test and debug an app Test the app with invalid data 1. Start Android Studio and open the project named ch04_ex _TipCalculator that's in this directory murachlandroidlex starts 2. Run this project and test the app with a valid subtotal like 100. This should work correctly 3. Test the app with an invalid subtotal by leaving the bill amount blank and pres sing the Done key. This should cause the app to crash with a run-time error, and it should display an error message in the LogCat window Study the error message that's displayed in red. You can focus on the first few lines of this message. These lines give information about the exception 4. tha t caused the app to crash. Based on this information, you should be able to figure out that the app crashed because an empty string isn't a valid float value 5. Fix the bug by using a try/catch statement to handle the exception. The catch clause for the exception should set the billAmount variable to zero Use LogCat logging 6. At the end of the onCreate method, add a logging statement that prints "onCreate executed" to the LogCat window. Add logging statements that print "onPause executed" and "onResume executed" to the ends of the onPause and onResume methods. Run the app and view the logging statements as you change orientation Note that this causes the onPause, onCreate, and onResume methods to be executed. This shows that the activity is destroyed and recreated when you change orientation Run the app and view the logging statements as you navigate away from and back to the app. Note that this only causes the onPause and onResume methods to be executed. This shows that the activity is paused when you 7. 8. 9. navigate away from it and resumed when you navigate back to it. Test on different emulators 10. Create an emulator for a tablet. 11. Run the app in that emulator. It should work as before 12. Create an emulator for a phone with a hard keyboard and a DPad 13. Run the app in that emulator. You should be able to use your computer'!s keyboard to enter a bill amount and press the Enter key to submit this entry. Chapter 4 How to test and debug an Android app 115 Exercise 4-1 Test and debug the Tip Calculator app This exercise guides you through the process of using Andr oid Studio to test and debug an app Test the app with invalid data 1. Start Android Studio and open the project named ch04_ex _TipCalculator that's in this directory murachlandroidlex starts 2. Run this project and test the app with a valid subtotal like 100. This should work correctly 3. Test the app with an invalid subtotal by leaving the bill amount blank and pres sing the Done key. This should cause the app to crash with a run-time error, and it should display an error message in the LogCat window Study the error message that's displayed in red. You can focus on the first few lines of this message. These lines give information about the exception 4. tha t caused the app to crash. Based on this information, you should be able to figure out that the app crashed because an empty string isn't a valid float value 5. Fix the bug by using a try/catch statement to handle the exception. The catch clause for the exception should set the billAmount variable to zero Use LogCat logging 6. At the end of the onCreate method, add a logging statement that prints "onCreate executed" to the LogCat window. Add logging statements that print "onPause executed" and "onResume executed" to the ends of the onPause and onResume methods. Run the app and view the logging statements as you change orientation Note that this causes the onPause, onCreate, and onResume methods to be executed. This shows that the activity is destroyed and recreated when you change orientation Run the app and view the logging statements as you navigate away from and back to the app. Note that this only causes the onPause and onResume methods to be executed. This shows that the activity is paused when you 7. 8. 9. navigate away from it and resumed when you navigate back to it. Test on different emulators 10. Create an emulator for a tablet. 11. Run the app in that emulator. It should work as before 12. Create an emulator for a phone with a hard keyboard and a DPad 13. Run the app in that emulator. You should be able to use your computer'!s keyboard to enter a bill amount and press the Enter key to submit this entry
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started