Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Hello, I need help with an assignment from murah's Android programming 2nd edition. It is chapter 6 example 3. We are supposed to modify the
Hello,
I need help with an assignment from murah's Android programming 2nd edition. It is chapter 6 example 3. We are supposed to modify the Tip Calculator app thats presented in the chapter so it uses anonymous inner classes for event listeners. I don't know how to do this though as I am not sure where it needs changed or how to. The EditText, SeekBar, RadioGroup, and Spinner listeners are what are supposed to be changed to being anonymous inner classes. Here is the code.
package com.murach.tipcalculator;
import java.text.NumberFormat; import android.os.Bundle; import android.view.KeyEvent; import android.view.View; import android.view.View.OnKeyListener; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.widget.AdapterView; import android.widget.AdapterView.OnItemSelectedListener; import android.widget.ArrayAdapter; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.RadioGroup.OnCheckedChangeListener; import android.widget.SeekBar; import android.widget.SeekBar.OnSeekBarChangeListener; import android.widget.Spinner; import android.widget.TextView; import android.widget.TextView.OnEditorActionListener; import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; public class TipCalculatorActivity extends Activity implements OnEditorActionListener, OnSeekBarChangeListener, OnCheckedChangeListener, OnItemSelectedListener, OnKeyListener { // define variables for the widgets private EditText billAmountEditText; private TextView percentTextView; private SeekBar percentSeekBar; private TextView tipTextView; private TextView totalTextView; private RadioGroup roundingRadioGroup; private RadioButton roundNoneRadioButton; private RadioButton roundTipRadioButton; private RadioButton roundTotalRadioButton; private Spinner splitSpinner; private TextView perPersonLabel; private TextView perPersonTextView; // define the SharedPreferences object private SharedPreferences savedValues; // define rounding constants private final int ROUND_NONE = 0; private final int ROUND_TIP = 1; private final int ROUND_TOTAL = 2; // define instance variables private String billAmountString = ""; private float tipPercent = .15f; private int rounding = ROUND_NONE; private int split = 1; @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); percentSeekBar = (SeekBar) findViewById(R.id.percentSeekBar); tipTextView = (TextView) findViewById(R.id.tipTextView); totalTextView = (TextView) findViewById(R.id.totalTextView); roundingRadioGroup = (RadioGroup) findViewById(R.id.roundingRadioGroup); roundNoneRadioButton = (RadioButton) findViewById(R.id.roundNoneRadioButton); roundTipRadioButton = (RadioButton) findViewById(R.id.roundTipRadioButton); roundTotalRadioButton = (RadioButton) findViewById(R.id.roundTotalRadioButton); splitSpinner = (Spinner) findViewById(R.id.splitSpinner); perPersonLabel = (TextView) findViewById(R.id.perPersonLabel); perPersonTextView = (TextView) findViewById(R.id.perPersonTextView); // set array adapter for spinner ArrayAdapteradapter = ArrayAdapter.createFromResource( this, R.array.split_array, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource( android.R.layout.simple_spinner_dropdown_item); splitSpinner.setAdapter(adapter); // set the listeners billAmountEditText.setOnEditorActionListener(this); billAmountEditText.setOnKeyListener(this); percentSeekBar.setOnSeekBarChangeListener(this); percentSeekBar.setOnKeyListener(this); roundingRadioGroup.setOnCheckedChangeListener(this); roundingRadioGroup.setOnKeyListener(this); splitSpinner.setOnItemSelectedListener(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.putInt("rounding", rounding); editor.putInt("split", split); editor.commit(); super.onPause(); } @Override public void onResume() { super.onResume(); // get the instance variables billAmountString = savedValues.getString("billAmountString", ""); tipPercent = savedValues.getFloat("tipPercent", 0.15f); rounding = savedValues.getInt("rounding", ROUND_NONE); split = savedValues.getInt("split", 1); // set the bill amount on its widget billAmountEditText.setText(billAmountString); // set the tip percent on its widget int progress = Math.round(tipPercent * 100); percentSeekBar.setProgress(progress); // set rounding on radio buttons // NOTE: this executes the onCheckedChanged method, // which executes the calculateAndDisplay method if (rounding == ROUND_NONE) { roundNoneRadioButton.setChecked(true); } else if (rounding == ROUND_TIP) { roundTipRadioButton.setChecked(true); } else if (rounding == ROUND_TIP) { roundTotalRadioButton.setChecked(true); } // set split on spinner // NOTE: this executes the onItemSelected method, // which executes the calculateAndDisplay method int position = split - 1; splitSpinner.setSelection(position); } public void calculateAndDisplay() { // get the bill amount billAmountString = billAmountEditText.getText().toString(); float billAmount; if (billAmountString.equals("")) { billAmount = 0; } else { billAmount = Float.parseFloat(billAmountString); } // get tip percent int progress = percentSeekBar.getProgress(); tipPercent = (float) progress / 100; // calculate tip and total float tipAmount = 0; float totalAmount = 0; if (rounding == ROUND_NONE) { tipAmount = billAmount * tipPercent; totalAmount = billAmount + tipAmount; } else if (rounding == ROUND_TIP) { tipAmount = StrictMath.round(billAmount * tipPercent); totalAmount = billAmount + tipAmount; } else if (rounding == ROUND_TOTAL) { float tipNotRounded = billAmount * tipPercent; totalAmount = StrictMath.round(billAmount + tipNotRounded); tipAmount = totalAmount - billAmount; } // calculate split amount and show/hide split amount widgets float splitAmount = 0; if (split == 1) { // no split - hide widgets perPersonLabel.setVisibility(View.GONE); perPersonTextView.setVisibility(View.GONE); } else { // split - calculate amount and show widgets splitAmount = totalAmount / split; perPersonLabel.setVisibility(View.VISIBLE); perPersonTextView.setVisibility(View.VISIBLE); } // display the results with formatting NumberFormat currency = NumberFormat.getCurrencyInstance(); tipTextView.setText(currency.format(tipAmount)); totalTextView.setText(currency.format(totalAmount)); perPersonTextView.setText(currency.format(splitAmount)); NumberFormat percent = NumberFormat.getPercentInstance(); percentTextView.setText(percent.format(tipPercent)); } //***************************************************** // Event handler for the EditText //***************************************************** @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED) { calculateAndDisplay(); } return false; } //***************************************************** // Event handler for the SeekBar //***************************************************** @Override public void onStartTrackingTouch(SeekBar seekBar) { // TODO Auto-generated method stub } @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { percentTextView.setText(progress + "%"); } @Override public void onStopTrackingTouch(SeekBar seekBar) { calculateAndDisplay(); } //***************************************************** // Event handler for the RadioGroup //***************************************************** @Override public void onCheckedChanged(RadioGroup group, int checkedId) { switch (checkedId) { case R.id.roundNoneRadioButton: rounding = ROUND_NONE; break; case R.id.roundTipRadioButton: rounding = ROUND_TIP; break; case R.id.roundTotalRadioButton: rounding = ROUND_TOTAL; break; } calculateAndDisplay(); } //***************************************************** // Event handler for the Spinner //***************************************************** @Override public void onItemSelected(AdapterView> parent, View v, int position, long id) { split = position + 1; calculateAndDisplay(); } @Override public void onNothingSelected(AdapterView> parent) { // Do nothing } //***************************************************** // Event handler for the keyboard and DPad //***************************************************** @Override public boolean onKey(View view, int keyCode, KeyEvent event) { switch (keyCode) { case KeyEvent.KEYCODE_ENTER: case KeyEvent.KEYCODE_DPAD_CENTER: calculateAndDisplay(); // hide the soft keyboard InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow( billAmountEditText.getWindowToken(), 0); // consume the event return true; case KeyEvent.KEYCODE_DPAD_RIGHT: case KeyEvent.KEYCODE_DPAD_LEFT: if (view.getId() == R.id.percentSeekBar) { calculateAndDisplay(); } break; } // don't consume the event return false; }
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