Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Could you please help me on this Android Mobile Application please. Appreciate your help Use an EditTextPreference element In this exercise, youll modify the Tip

Could you please help me on this Android Mobile Application please. Appreciate your help

Use an EditTextPreference element In this exercise, youll modify the Tip Calculator app presented in this chapter so it uses an EditTextPreference element. Although this type of preference wasnt described in this chapter, it works similarly to the CheckBoxPreference element, and you can look it up in the documentation for the API if necessary. When youre done, a test run should look like this: 1. Start Android Studio and open the project named ch08_ex4_TipCalculator.

1. Open the preferences.xml file in the xml directory. Add an EditTextPreference element for a setting named Name. This setting should allow the user to enter his or her full name.

2. Run the app and use the Settings activity to enter your name.

3. Open the layout of the activity. Modify the layout, so it includes a fifth row that can display a name. Open the class for the Tip Calculator activity. Modify this code so it gets the name from the preferences and displays that name in t

The following files are given; need to fix the above

Android Studio: Files Under Java

package com.murach.tipcalculator;

import android.os.Bundle; import android.app.Activity; public class AboutActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_about); } }

package com.murach.tipcalculator; import android.app.Activity; import android.os.Bundle; public class SettingsActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Display the fragment as the main content. getFragmentManager().beginTransaction() .replace(android.R.id.content, new SettingsFragment()) .commit(); } } 

package com.murach.tipcalculator; import android.os.Bundle; import android.preference.PreferenceFragment; public class SettingsFragment extends PreferenceFragment { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // Load the preferences from an XML resource addPreferencesFromResource(R.xml.preferences); } } 

Under layout: TipCalculatorActivity

package layout; import java.text.NumberFormat; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.KeyEvent; import android.view.Menu; import android.view.MenuItem; 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.Intent; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import com.murach.tipcalculator.AboutActivity; import com.murach.tipcalculator.SettingsActivity; 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 instance variables that should be saved private String billAmountString = ""; private float tipPercent = .15f; // define rounding constants private final int ROUND_NONE = 0; private final int ROUND_TIP = 1; private final int ROUND_TOTAL = 2; // set up preferences private SharedPreferences prefs; private boolean rememberTipPercent = true; private int rounding = ROUND_NONE; @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); // set the default values for the preferences PreferenceManager.setDefaultValues(this, R.xml.preferences, false); // get default SharedPreferences object prefs = PreferenceManager.getDefaultSharedPreferences(this); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.activity_tip_calculator, menu); return true; } @Override public void onPause() { // save the instance variables Editor editor = prefs.edit(); editor.putString("billAmountString", billAmountString); editor.putFloat("tipPercent", tipPercent); editor.commit(); super.onPause(); } @Override public void onResume() { super.onResume(); // get preferences rememberTipPercent = prefs.getBoolean("pref_remember_percent", true); rounding = Integer.parseInt(prefs.getString("pref_rounding", "0")); // get the instance variables billAmountString = prefs.getString("billAmountString", ""); if (rememberTipPercent) { tipPercent = prefs.getFloat("tipPercent", 0.15f); } else { 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 = 0; float totalAmount = 0; float tipPercentToDisplay = 0; if (rounding == ROUND_NONE) { tipAmount = billAmount * tipPercent; totalAmount = billAmount + tipAmount; tipPercentToDisplay = tipPercent; } else if (rounding == ROUND_TIP) { tipAmount = StrictMath.round(billAmount * tipPercent); totalAmount = billAmount + tipAmount; tipPercentToDisplay = tipAmount / billAmount; } else if (rounding == ROUND_TOTAL) { float tipNotRounded = billAmount * tipPercent; totalAmount = StrictMath.round(billAmount + tipNotRounded); tipAmount = totalAmount - billAmount; tipPercentToDisplay = tipAmount / billAmount; } // 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(tipPercentToDisplay)); } @Override public boolean onEditorAction(TextView v, int actionId, KeyEvent event) { int keyCode = -1; if (event != null) { keyCode = event.getKeyCode(); } if (actionId == EditorInfo.IME_ACTION_DONE || actionId == EditorInfo.IME_ACTION_UNSPECIFIED || keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == KeyEvent.KEYCODE_ENTER) { 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; } } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_settings: // Toast.makeText(this, "Settings", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(), SettingsActivity.class)); return true; case R.id.menu_about: // Toast.makeText(this, "About", Toast.LENGTH_SHORT).show(); startActivity(new Intent(getApplicationContext(), AboutActivity.class)); return true; default: return super.onOptionsItemSelected(item); } } } 

Under Value: strings.xml

xml version="1.0" encoding="utf-8"?> <resources> <string name="app_name">Tip Calculatorstring> <string name="title_activity_tip_calculator">Tip Calculatorstring> <string name="bill_amount_label">Bill Amountstring> <string name="bill_amount">string> <string name="tip_percent_label">Percentstring> <string name="tip_percent">15%string> <string name="increase">+string> <string name="decrease">-string> <string name="tip_amount_label">Tipstring> <string name="tip_amount">$0.00string> <string name="total_amount_label">Totalstring> <string name="total_amount">$0.00string> <string name="menu_settings">Settingsstring> <string name="menu_about">Aboutstring> <string name="title_activity_settings">Settingsstring> <string name="remember_percent_title">Remember Tip Percentstring> <string name="remember_percent_summary">Remembers the tip percent for the most recent calculation.string> <string name="rounding_title">Rounding?string> <string name="rounding_summary">Sets the rounding option for the calculation.string> <string-array name="rounding_keys"> <item >Noneitem> <item >Tipitem> <item >Totalitem> string-array> <string-array name="rounding_values"> <item >0item> <item >1item> <item >2item> string-array> <string name="rounding_default">0string> <string name="title_activity_about">Aboutstring> <string name="about">This version of the Tip Calculator app calculates all tips on the post-tax total for the bill.string> resources> 

Under Value: style.xml

<resources> <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" /> <style name="DialogTheme" parent="android:Theme.Holo.Light.Dialog" /> resources> 

Under xml: preference.xml

This doesnt have anything. This is what has to build

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Advances In Database Technology Edbt 88 International Conference On Extending Database Technology Venice Italy March 14 18 1988 Proceedings Lncs 303

Authors: Joachim W. Schmidt ,Stefano Ceri ,Michele Missikoff

1988th Edition

3540190740, 978-3540190745

More Books

Students also viewed these Databases questions

Question

5. If yes, then why?

Answered: 1 week ago

Question

6. How would you design your ideal position?

Answered: 1 week ago