package io.fictional.qa.tipthis;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.View.OnClickListener;

import java.text.NumberFormat;
import java.util.Locale;

public class MyActivity extends Activity {

    private TextView tipAmount15percent, totalWith15Percent;
    private TextView tipAmount20percent, totalWith20Percent;
    private TextView tipAmount25percent, totalWith25Percent;
    private String savedMealTotalString;
    private TextView mealTotalField;

    NumberFormat currencyFormatter = NumberFormat.getCurrencyInstance(Locale.getDefault());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my);

        GetTextFields();
        Button btnCalculateTip = (Button) findViewById(R.id.btnCalculate);
        Button btnClear = (Button) findViewById(R.id.btnClear);
        mealTotalField = (TextView) findViewById(R.id.mealTotalInput);

        // TODO
        // stop you putting in three or more decimal places, use Currency Formatter on text field
        // recalculate after screen rotation
        // do the calculation when you hit done on the keyboard

        btnCalculateTip.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                CheckIfTotalIsEmpty(mealTotalField.getText().toString());
            }
        });

        btnClear.setOnClickListener(new OnClickListener() {
            public void onClick(View v) {
                ClearAllFields();
            }
        });

        // Check whether we're recreating a previously destroyed instance
        if (savedInstanceState != null) {
            // Restore value of members from saved state
            CheckIfTotalIsEmpty(savedInstanceState.getString(savedMealTotalString));
        }
    }

    @Override
    public void onSaveInstanceState(Bundle savedInstanceState) {
        // Save the current state
        savedInstanceState.putString(savedMealTotalString, mealTotalField.getText().toString());
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(savedInstanceState);
    }

    private void ClearAllFields() {
        mealTotalField.setText("");
        tipAmount15percent.setText("15%");
        totalWith15Percent.setText(" ");
        tipAmount20percent.setText("20%");
        totalWith20Percent.setText(" ");
        tipAmount25percent.setText("25%");
        totalWith25Percent.setText(" ");
    }

    private void CheckIfTotalIsEmpty(String mealTotalString) {
        if (!mealTotalString.isEmpty())
        {
            CalculateTip(Double.parseDouble(mealTotalString));
        }
    }

    @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.
        int id = item.getItemId();
        return id == R.id.action_about || super.onOptionsItemSelected(item);
    }

    private void CalculateTip(double mealTotal)
    {
        tipAmount15percent.setText(String.format("%s", "15%: " + currencyFormatter.format((mealTotal * .15))));
        totalWith15Percent.setText(String.format("%s", "Total: " + currencyFormatter.format((mealTotal * 1.15))));

        tipAmount20percent.setText(String.format("%s", "20%: " + currencyFormatter.format((mealTotal * .2))));
        totalWith20Percent.setText(String.format("%s", "Total: " + currencyFormatter.format((mealTotal *1.2))));

        tipAmount25percent.setText(String.format("%s", "25%: " + currencyFormatter.format((mealTotal * .25))));
        totalWith25Percent.setText(String.format("%s", "Total: " + currencyFormatter.format((mealTotal *1.25))));
    }

    private void GetTextFields()
    {
        tipAmount15percent = (TextView) findViewById(R.id.txt15percenttip);
        tipAmount20percent = (TextView) findViewById(R.id.txt20percenttip);
        tipAmount25percent = (TextView) findViewById(R.id.txt25percenttip);
        totalWith15Percent = (TextView) findViewById(R.id.txt15percenttotal);
        totalWith20Percent = (TextView) findViewById(R.id.txt20percenttotal);
        totalWith25Percent = (TextView) findViewById(R.id.txt25percenttotal);
    }
}

@@ -86,16 +86,81 @@
 undle;%0D%0A
+import android.text.Editable;%0D%0Aimport android.text.TextWatcher;%0D%0A
 import a
@@ -178,16 +178,16 @@
 .Menu;%0D%0A
-
 import a
@@ -1344,54 +1344,8 @@
 ld%0D%0A
-        // recalculate after screen rotation%0D%0A
     
@@ -1804,176 +1804,766 @@
     
-// Check whether we're recreating a previously destroyed
+mealTotalField.addTextChangedListener(new TextWatcher() %7B%0D%0A            @Override%0D%0A            public void beforeTextChanged(CharSequence s, int start, int count, int after) %7B%0D%0A                // do nothing%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            public void onTextChanged(CharSequence s,
  in
+t 
 sta
-nce%0D%0A        if (savedInstanceState != null) %7B%0D%0A      
+rt, int before, int count) %7B%0D%0A                //do nothing%0D%0A            %7D%0D%0A%0D%0A            @Override%0D%0A            public void afterTextChanged(Editable s) %7B%0D%0A                CheckIfTotalIsEmpty(mealTotalField.getText().toString());%0D%0A            %7D%0D%0A        %7D);%0D%0A%0D%0A
       
+  
 // 
-Restore value of members from 
+Check if recreating a previously destroyed instance%0D%0A        // rendered unnecessary because of the textWatcher on the edit field%0D%0A//        if (
 saved
- state%0D%0A
+InstanceState != null) %7B%0D%0A//
     
@@ -2636,32 +2636,34 @@
 TotalString));%0D%0A
+//
         %7D%0D%0A    %7D

Add textWatcher