Android Open Source - Android-TipCalculator Tip Calculator Activity






From Project

Back to project page Android-TipCalculator.

License

The source code is released under:

Apache License

If you think the Android project Android-TipCalculator 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.yahoo.techlearn.tipcalculator;
//from   w  w w  . ja v a 2s .  co m
import java.text.DecimalFormat;

import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;
import android.widget.Toast;

public class TipCalculatorActivity extends Activity {
  private EditText etAmount;
  private TextView tvTipAmount;
  private TextView tvTipPercentage;
  private SeekBar skBar;
  private double totalAmount;
  private double tipPercentage;
  private int skBarProgress;
  private DecimalFormat decFormat;
  private final String invalidAmountMsg = "Not a valid amount, please try again.";
  
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tip_calculator);
        etAmount = (EditText) findViewById(R.id.etAmount);
        tvTipAmount = (TextView) findViewById(R.id.tvTipAmount);
        tvTipPercentage = (TextView) findViewById(R.id.tvTipPercentage);
        skBar = (SeekBar) findViewById(R.id.skBar);
        decFormat = new DecimalFormat("#.00");
        setupEditTextListener();
        setupSeekBarListener();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.tip_calculator, menu);
        return true;
    }   

    private void setupEditTextListener() {
      etAmount.addTextChangedListener(new TextWatcher(){

      @Override
      public void afterTextChanged(Editable s) {
           try {
             totalAmount = Double.valueOf(etAmount.getText().toString());
          tvTipAmount.setText("$" + decFormat.format(totalAmount * skBarProgress * 0.01));
           } catch (NumberFormatException e) {
             Toast.makeText(getBaseContext(), invalidAmountMsg, Toast.LENGTH_SHORT).show();
            return;
           }
      }

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

      @Override
      public void onTextChanged(CharSequence s, int start, int before,
          int count) {
          findViewById(R.id.btn10perc).setEnabled(true);
          findViewById(R.id.btn15perc).setEnabled(true);
          findViewById(R.id.btn20perc).setEnabled(true);
      }
        
      });      
  }
    
    private void setupSeekBarListener() {
      skBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {

      @Override
      public void onProgressChanged(SeekBar seekBar, int progress,
          boolean fromUser) {
        tvTipPercentage.setText(progress + "%");
      }

      @Override
      public void onStartTrackingTouch(SeekBar seekBar) {
      }

      @Override
      public void onStopTrackingTouch(SeekBar seekBar) {
          try {
              totalAmount = Double.valueOf(etAmount.getText().toString());
              skBarProgress = seekBar.getProgress();
            tvTipAmount.setText("$" + decFormat.format(totalAmount * skBarProgress * 0.01));
          } catch (NumberFormatException e) {
            Toast.makeText(getBaseContext(), invalidAmountMsg, Toast.LENGTH_SHORT).show();
            return;
          }
      }
        
      });
    }
    
    public void onClick(View v) {
      try {
        totalAmount = Double.valueOf(etAmount.getText().toString());
          tipPercentage = Double.valueOf(v.getTag().toString());
          tvTipAmount.setText("$" + decFormat.format(totalAmount * tipPercentage));
          skBarProgress = (int) (tipPercentage * 100);
          tvTipPercentage.setText(skBarProgress + "%");
          skBar.setProgress(skBarProgress);
      } catch (NumberFormatException e) {
        Toast.makeText(this, invalidAmountMsg, Toast.LENGTH_SHORT).show();
        return;
      }      
    }
}




Java Source Code List

com.yahoo.techlearn.tipcalculator.TipCalculatorActivity.java