Android Open Source - Sales_Tax_Calculator Main






From Project

Back to project page Sales_Tax_Calculator.

License

The source code is released under:

Apache License

If you think the Android project Sales_Tax_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.salestax;
//www. jav a  2s .  com
import android.app.ActionBar;
import android.app.Dialog;
import android.app.SearchManager;
import android.content.Context;
import android.content.Intent;
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.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DecimalFormat;

public class Main extends ActionBarActivity {

    // UI elements
    TextView taxOutput, totalPriceOutput;
    EditText priceInput, rateInput;
    ImageButton searchButton;

    Context context = this;

    String searchString;

    float rate, price;

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

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

        taxOutput = (TextView) findViewById(R.id.taxOutput);
        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");
                    taxOutput.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);
                    }

                }

            }

        });

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

             if (rateInput.length() == 0) {
                 totalPriceOutput.setText("0.00");
                 taxOutput.setText("0.00");
             }

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

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

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

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

                 }

                 if (rateInput.getText().length() >= 1) {
                     calculateTax();
                 }

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

             }

         }

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

         @Override
         public void afterTextChanged(Editable s) {
         }

        });

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

        searchButton = (ImageButton) findViewById(R.id.searchButton);
        searchButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final Dialog dialog = new Dialog(context);
                dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
                dialog.setContentView(R.layout.searchdialog);

                final EditText locationInput = (EditText) dialog.findViewById(R.id.locationIput);
                locationInput.requestFocus();
                getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

                Button b1 = (Button) dialog.findViewById(R.id.button1);
                b1.setOnClickListener(new View.OnClickListener() {
                    public void onClick(View v) {

                        if (locationInput.getText().toString().trim().length() > 0) {
                            searchString = "What is the sales tax rate in " + locationInput.getText().toString().trim();

                            Intent intent = new Intent(Intent.ACTION_WEB_SEARCH);
                            intent.putExtra(SearchManager.QUERY, searchString);
                            context.startActivity(intent);

                            dialog.dismiss();
                        } else {
                            // this is never called... don't know why
                            Toast.makeText(context, "Please enter a location.", Toast.LENGTH_SHORT);
                        }

                    }
                });

                dialog.show();

            }
        });

    }

    public void calculateTax() {

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

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

        double totalTax = (rate * price);

        DecimalFormat moneyFormat = new DecimalFormat("0.00");

        taxOutput.setText(String.valueOf(moneyFormat.format(rate * price)));

        totalPriceOutput.setText(String.valueOf(moneyFormat.format(totalTax + price)));
    }

    @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", rateInput.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) {
            rateInput.setText(String.valueOf(preferences.getString("rateInput", "")));
        }
    }
}




Java Source Code List

com.collinguarino.salestax.Main.java