Android Open Source - Discount_Calculator Main






From Project

Back to project page Discount_Calculator.

License

The source code is released under:

Apache License

If you think the Android project Discount_Calculator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.collinguarino.discountcalculator;
/*w ww.ja  v  a2s.  c om*/
import android.app.ActionBar;
import android.content.SharedPreferences;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.view.View;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import java.text.DecimalFormat;

public class Main extends ActionBarActivity {

    // UI Elements
    TextView savingsOutput, totalPriceOutput;
    EditText priceInput, discountInput;

    // Calc Vars
    float rate, price;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.discount_calc);

        if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            ActionBar actionBar = getActionBar();
            actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#00a6e8")));
        }

        savingsOutput = (TextView) findViewById(R.id.savingsOutput);
        totalPriceOutput = (TextView) findViewById(R.id.totalPriceOutput);

        priceInput = (EditText) findViewById(R.id.priceInput);
        priceInput.requestFocus();
        getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        priceInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                // Fixed bug where bill amount with nothing in it was generating the previous calculation
                if (priceInput.length() == 0) {
                    totalPriceOutput.setText("0.00");
                    savingsOutput.setText("0.00");
                }

                // Fixes bug where entering "." first would crash
                if (priceInput.getText().toString().startsWith(".")) {
                    priceInput.setText("0.");

                    int len = priceInput.getEditableText().toString().trim().length();

                    if (len > 1) {
                        priceInput.setSelection((int) len);
                    }

                }

            }
        });

        discountInput = (EditText) findViewById(R.id.discountInput);
        discountInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

                if (discountInput.length() == 0) {
                    totalPriceOutput.setText("0.00");
                    savingsOutput.setText("0.00");
                }

                if (discountInput.getText().length() > 0) {

                    // Fixes bug where entering "." first would crash
                    if (discountInput.getText().toString().startsWith(".")) {
                        discountInput.setText("0.");

                        int len = discountInput.getEditableText().toString().trim().length();

                        if (len > 1) {
                            discountInput.setSelection((int) len);
                        }

                    }

                    if (discountInput.getText().length() >= 1) {
                        calculateSavings();
                    }

                    Editable etext = (Editable) discountInput.getText();
                    Selection.setSelection(etext, etext.length());

                }

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
            }

        });

        discountInput.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (discountInput.hasFocus()) {
                    discountInput.setHint("");
                } else {
                    discountInput.setHint("0.0 %");
                }
            }
        });
    }

    public void calculateSavings() {

        if (discountInput.getText().length() > 0) {
            rate = (Float.parseFloat(discountInput.getText().toString())) / 100;
        }

        if (priceInput.getText().length() > 0) {
            price = (Float.parseFloat(priceInput.getText().toString()));
        }

        double totalSavings = (rate * price);

        DecimalFormat moneyFormat = new DecimalFormat("0.00");

        savingsOutput.setText(String.valueOf(moneyFormat.format(totalSavings)));

        totalPriceOutput.setText(String.valueOf(moneyFormat.format(price - totalSavings)));
    }

    @Override
    protected void onPause() {
        super.onPause();

        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        SharedPreferences.Editor editor = preferences.edit();
        editor.putString("priceInput", priceInput.getText().toString());
        editor.putString("rateInput", discountInput.getText().toString());
        editor.commit();

    }

    @Override
    protected void onResume() {
        super.onResume();

        SharedPreferences preferences = PreferenceManager
                .getDefaultSharedPreferences(this);

        if (preferences.getString("priceInput", "") != null) {
            priceInput.setText(String.valueOf(preferences.getString("priceInput", "")));
        }
        if (preferences.getString("rateInput", "") != null) {
            discountInput.setText(String.valueOf(preferences.getString("rateInput", "")));
        }
    }

}




Java Source Code List

com.collinguarino.discountcalculator.Main.java