Question
I have included the code I have so far for my tip calculator app. I just need help adding LogCat. 1. Use LogCat logging a.
I have included the code I have so far for my tip calculator app. I just need help adding LogCat.
1. Use LogCat logging
- a. Override the onResume method and use it to call the calculateAndDisplay method.
- b. Press the Back key to navigate away from the app. Then, navigate back to the app. In an emulator, click on the Apps icon and click on the Invoice Total app. The activity should lose all of its data.
- c. Override the onPause method so it saves the string for the subtotal. Then, modify the onResume method so it gets the string for the subtotal. To get these methods to work correctly, you need to set up instance variables for the subtotal string and for a SharedPreferences object that you can use to save and get this string.
- d. Set a launcher icon for the app. You should be able to download possible icons by searching the Internet. When you do that, make sure you have permission to use the image or that it is available under a license that allows you to use it legally.
- e. At the end of the onCreate method, add a logging statement that prints "onCreate executed" to the LogCat window.
- f. Add logging statements that print in all methods to describe the method is running. For instance, "onCreate executed" in onCreate methods.
- g. Run the app and view the logging statements as you change orientation.
- h. Add logging all values of variables such as total, bill amount, and total amount etc to make sure if you received proper parameters and your calculation with the parameters is correct.
Java:
package com.murach.invoice;
import java.text.NumberFormat;
import android.app.Activity;
import android.os.Bundle;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
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.widget.Toast;
public class InvoiceTotalActivity extends Activity implements OnEditorActionListener, OnClickListener {
//define variables for widgets
private EditText billAmountEditText;
private TextView percentTextView;
private Button percentUpButton;
private Button percentDownButton;
private TextView tipTextView;
private TextView totalTextView;
//define SharedPreferences object
private SharedPreferences savedValues;
//define an instance variable for tip percent
private String billAmountString = "";
private float tipPercent = 15f;
//Toast.makeText(this, "Initial Amount: ")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_invoice_total);
//get references to 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 instance variables
SharedPreferences.Editor editor = savedValues.edit();
editor.putString("billAmountString", billAmountString);
editor.putFloat("tipPercent", tipPercent);
editor.apply();
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;
if (billAmountString.equals("")) {
billAmount = 0;
} else {
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)
{
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;
}
}
}
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