Question: Android Studio Extend the Shipping Calculator application from the class to include the following: 1.Add three small text input fields to request for package dimensions,
Android Studio
Extend the Shipping Calculator application from the class to include the following:
1.Add three small text input fields to request for package dimensions, length x width x height, in whole number of inches. The package size will affect the base cost only. If all three dimensions are no more than 12 inches, there is no change to the base cost. If the largest dimension is over 12 inches but no more than 24 inches, the base cost will be doubled. If the largest dimension is over 24 inches, the base cost will be three times as much as the current amount. The initial and default package size should be set to 12 x 12 x 12.
2.Add a group of three radio buttons to indicate the shipping service desired: Express, Standard, or Ground. There is no change in the cost calculation for ground shipping. For standard shipping service, both the base cost and the added cost will be doubled. For express service, the base cost and added cost will be tripled. Use Standard shipping as the initial and default service for cost calculation.
Design and layout the additional components to maintain consistency with the current user
interface.
The code is as follows:
activity_main.xml file
***************************************************
package com.cornez.shippingcalculator; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; public class MyActivity extends Activity { //DATA MODEL FOR SHIP ITEM private ShipItem shipItem; //VIEW OBJECTS FOR LAYOUT UI REFERENCE private EditText weightET; private TextView baseCostTV; private TextView addedCostTV; private TextView totalCostTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_2); //CREATE THE DATA MODEL FOR STORING THE ITEM TO BE SHIPPED shipItem = new ShipItem(); //TASK 3: ESTABLISH THE REFERENCES TO INPUT WEIGHT ELEMENT weightET = (EditText) findViewById(R.id.editText1); //TASK 3: ESTABLISH THE REFERENCES TO OUTPUT ELEMENTS baseCostTV = (TextView) findViewById(R.id.textView4); addedCostTV = (TextView) findViewById(R.id.textView6); totalCostTV = (TextView) findViewById(R.id.textView8); //TASK 4: REGISTER THE LISTENER EVENT FOR WEIGHT INPUT weightET.addTextChangedListener(weightTextWatcher); } private TextWatcher weightTextWatcher = new TextWatcher() { //THE INPUT ELEMENT IS ATTACHED TO AN EDITABLE, //THEREFORE THESE METHODS ARE CALLED WHEN THE TEXT IS CHANGED public void onTextChanged(CharSequence s, int start, int before, int count){ //CATCH AN EXCEPTION WHEN THE INPUT IS NOT A NUMBER try { shipItem.setWeight((int) Double.parseDouble(s.toString())); }catch (NumberFormatException e){ shipItem.setWeight(0); } displayShipping(); } public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after){} }; private void displayShipping() { //DISPLAY THE BASE COST, ADDED COST, AND TOTAL COST baseCostTV.setText("$" + String.format("%.02f", shipItem.getBaseCost())); addedCostTV.setText("$" + String.format("%.02f", shipItem.getAddedCost())); totalCostTV.setText("$" + String.format("%.02f", shipItem.getTotalCost())); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } activity_main_2.xml file
***************************************************
xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/shippingbck"> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="250dp" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_marginLeft="10dp" android:layout_marginTop="10dp" android:orientation="vertical" android:background="@drawable/paper"> <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_horizontal" android:text="@string/weightLBL" android:textAppearance="?android:attr/textAppearanceLarge" android:textSize="20sp" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_marginTop="5dp" android:ems="10" android:gravity="center_vertical|center_horizontal" android:inputType="number" android:selectAllOnFocus="true" android:textSize="35sp" android:hint="@string/zero"> <requestFocus /> EditText> <TextView android:id="@+id/textView2" android:layout_width="match_parent" android:layout_height="wrap_content" android:gravity="center_horizontal" android:text="@string/ouncesLBL" android:textAppearance="?android:attr/textAppearanceSmall" /> LinearLayout> <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginLeft="44dp" android:layout_marginTop="16dp" android:layout_toRightOf="@+id/linearLayout1" android:text="@string/baseLBL" android:textAppearance="?android:attr/textAppearanceMedium" android:paddingLeft="@dimen/output_margin_buffer" /> <TextView android:id="@+id/textView4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView3" android:layout_alignBottom="@+id/textView3" android:layout_alignParentRight="true" android:paddingRight="@dimen/output_margin_buffer" android:text="@string/zeroDec" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView3" android:layout_below="@+id/textView3" android:layout_marginTop="16dp" android:text="@string/addCostLBL" android:textAppearance="?android:attr/textAppearanceMedium" android:paddingLeft="@dimen/output_margin_buffer" /> <TextView android:id="@+id/textView6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView5" android:layout_alignBottom="@+id/textView5" android:layout_alignParentRight="true" android:paddingRight="@dimen/output_margin_buffer" android:text="@string/zeroDec" android:textAppearance="?android:attr/textAppearanceMedium" /> <TextView android:id="@+id/textView7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignLeft="@+id/textView5" android:layout_below="@+id/textView5" android:layout_marginTop="16dp" android:text="@string/totalLBL" android:textAppearance="?android:attr/textAppearanceMedium" android:paddingLeft="@dimen/output_margin_buffer" /> <TextView android:id="@+id/textView8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBaseline="@+id/textView7" android:layout_alignBottom="@+id/textView7" android:layout_alignParentRight="true" android:paddingRight="@dimen/output_margin_buffer" android:text="@string/zeroDec" android:textAppearance="?android:attr/textAppearanceMedium" /> RelativeLayout>
******************************************MainActivity.java*******************************************
package com.cornez.shippingcalculator; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Menu; import android.view.MenuItem; import android.widget.EditText; import android.widget.TextView; public class MyActivity extends Activity { //DATA MODEL FOR SHIP ITEM private ShipItem shipItem; //VIEW OBJECTS FOR LAYOUT UI REFERENCE private EditText weightET; private TextView baseCostTV; private TextView addedCostTV; private TextView totalCostTV; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my_2); //CREATE THE DATA MODEL FOR STORING THE ITEM TO BE SHIPPED shipItem = new ShipItem(); //TASK 3: ESTABLISH THE REFERENCES TO INPUT WEIGHT ELEMENT weightET = (EditText) findViewById(R.id.editText1); //TASK 3: ESTABLISH THE REFERENCES TO OUTPUT ELEMENTS baseCostTV = (TextView) findViewById(R.id.textView4); addedCostTV = (TextView) findViewById(R.id.textView6); totalCostTV = (TextView) findViewById(R.id.textView8); //TASK 4: REGISTER THE LISTENER EVENT FOR WEIGHT INPUT weightET.addTextChangedListener(weightTextWatcher); } private TextWatcher weightTextWatcher = new TextWatcher() { //THE INPUT ELEMENT IS ATTACHED TO AN EDITABLE, //THEREFORE THESE METHODS ARE CALLED WHEN THE TEXT IS CHANGED public void onTextChanged(CharSequence s, int start, int before, int count){ //CATCH AN EXCEPTION WHEN THE INPUT IS NOT A NUMBER try { shipItem.setWeight((int) Double.parseDouble(s.toString())); }catch (NumberFormatException e){ shipItem.setWeight(0); } displayShipping(); } public void afterTextChanged(Editable s) {} public void beforeTextChanged(CharSequence s, int start, int count, int after){} }; private void displayShipping() { //DISPLAY THE BASE COST, ADDED COST, AND TOTAL COST baseCostTV.setText("$" + String.format("%.02f", shipItem.getBaseCost())); addedCostTV.setText("$" + String.format("%.02f", shipItem.getAddedCost())); totalCostTV.setText("$" + String.format("%.02f", shipItem.getTotalCost())); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.my, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } } Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
