In: Computer Science
Murach Android tip calculator event handling assignment
//MainActivity.java
package com.thephoenixzone.murachtipcalculator; import androidx.appcompat.app.AppCompatActivity; import android.content.SharedPreferences; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.inputmethod.EditorInfo; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import java.text.NumberFormat; public class MainActivity extends AppCompatActivity implements TextView.OnEditorActionListener, View.OnClickListener { // define variables for the widgets private EditText etbillAmount; private TextView txtPercent; private Button btnPercentUp; private Button btnPercentDown; private TextView txtTip; private TextView txtTotal; // define the SharedPreferences object private SharedPreferences savedValues; // define instance variables that should be saved private String billAmount = ""; private float tipPercent = .10f; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // get references to the widgets etbillAmount = (EditText) findViewById(R.id.billAmountEditText); txtPercent = (TextView) findViewById(R.id.percentTextView); btnPercentUp = (Button) findViewById(R.id.percentUpButton); btnPercentDown = (Button) findViewById(R.id.percentDownButton); txtTip = (TextView) findViewById(R.id.tipTextView); txtTotal = (TextView) findViewById(R.id.totalTextView); // set the listeners etbillAmount.setOnEditorActionListener(this); btnPercentUp.setOnClickListener(this); btnPercentDown.setOnClickListener(this); // get SharedPreferences object savedValues = getSharedPreferences("SavedValues", MODE_PRIVATE); } @Override public void onPause() { // save the instance variables SharedPreferences.Editor editor = savedValues.edit(); editor.putString("billAmount", billAmount); editor.putFloat("tipPercent", tipPercent); editor.commit(); super.onPause(); } @Override public void onResume() { super.onResume(); // get the instance variables billAmount = savedValues.getString("billAmount", ""); tipPercent = savedValues.getFloat("tipPercent", 0.15f); // set the bill amount on its widget etbillAmount.setText(billAmount); // calculate and display calculateAndDisplay(); } public void calculateAndDisplay() { // get the bill amount billAmount = etbillAmount.getText().toString(); float billAmount1; if (billAmount.equals("")) { billAmount1 = 0; } else { billAmount1 = Float.parseFloat(billAmount); } // calculate tip and total float tipAmount = billAmount1 * tipPercent; float totalAmount = billAmount1 + tipAmount; // display the other results with formatting NumberFormat currency = NumberFormat.getCurrencyInstance(); txtTip.setText(currency.format(tipAmount)); txtTotal.setText(currency.format(totalAmount)); NumberFormat percent = NumberFormat.getPercentInstance(); txtPercent.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; } } }
//activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:padding="10dp" > <!-- The bill amount --> <TextView android:id="@+id/billAmountLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:padding="10dp" android:text="@string/bill_amount_label" android:textSize="20sp" android:textStyle="bold" /> <EditText android:id="@+id/billAmountEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/billAmountLabel" android:layout_marginLeft="5dp" android:layout_toRightOf="@+id/billAmountLabel" android:ems="8" android:inputType="numberDecimal" android:text="@string/bill_amount" android:textSize="20sp" > <requestFocus /> </EditText> <!-- The tip percent --> <TextView android:id="@+id/percentLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/billAmountLabel" android:layout_below="@+id/billAmountLabel" android:padding="10dp" android:text="@string/tip_percent_label" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/percentTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/percentLabel" android:layout_alignLeft="@+id/billAmountEditText" android:padding="5dp" android:text="@string/tip_percent" android:textSize="20sp" /> <Button android:id="@+id/percentDownButton" android:layout_width="45dp" android:layout_height="45dp" android:layout_alignBaseline="@+id/percentTextView" android:layout_marginLeft="25dp" android:layout_toRightOf="@+id/percentTextView" android:text="@string/decrease" android:textSize="20sp" /> <Button android:id="@+id/percentUpButton" android:layout_width="45dp" android:layout_height="45dp" android:layout_alignBaseline="@+id/percentDownButton" android:layout_toRightOf="@+id/percentDownButton" android:text="@string/increase" android:textSize="20sp" /> <!-- the tip amount --> <TextView android:id="@+id/tipLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/percentLabel" android:layout_below="@+id/percentLabel" android:padding="10dp" android:text="@string/tip_amount_label" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/tipLabel" android:layout_alignLeft="@id/billAmountEditText" android:padding="5dp" android:text="@string/tip_amount" android:textSize="20sp" /> <!-- the total --> <TextView android:id="@+id/totalLabel" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/tipLabel" android:layout_below="@+id/tipLabel" android:padding="10dp" android:text="@string/total_amount_label" android:textSize="20sp" android:textStyle="bold" /> <TextView android:id="@+id/totalTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/totalLabel" android:layout_alignLeft="@+id/tipTextView" android:padding="5dp" android:text="@string/total_amount" android:textSize="20sp" /> </RelativeLayout>
//strings.xml
<resources> <string name="app_name">MurachTipCalculator</string> <string name="title_activity_tip_calculator">Tip Calculator</string> <string name="menu_settings">Settings</string> <string name="bill_amount_label">Bill Amount</string> <string name="bill_amount"></string> <string name="tip_percent_label">Percent</string> <string name="tip_percent">15%</string> <string name="increase">+</string> <string name="decrease">-</string> <string name="tip_amount_label">Tip</string> <string name="tip_amount">$0.00</string> <string name="total_amount_label">Total</string> <string name="total_amount">$0.00</string> </resources>
//Output