Back to project page SimpleTipCalculator.
The source code is released under:
MIT License
If you think the Android project SimpleTipCalculator listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.nickrasband.tipcalculator; //from w w w . ja v a 2 s. c o m import java.text.NumberFormat; import android.app.Activity; import android.os.Bundle; import android.text.Editable; import android.text.TextWatcher; import android.view.Gravity; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.Toast; public class TipActivity extends Activity { private EditText etAmount; private TextView tvTipAmount; private TextView tvTotalAmount; private Button lastButtonPressed; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_tip); lastButtonPressed = null; // Get references to all of the form elements. etAmount = (EditText)findViewById(R.id.etAmount); tvTipAmount = (TextView)findViewById(R.id.tvTipAmount); tvTotalAmount = (TextView)findViewById(R.id.tvTotalAmount); // Add a text changed listener to the transaction amount field to automatically update the tip/total. etAmount.addTextChangedListener(new TextWatcher() { @Override public void afterTextChanged(Editable s) { String amount = s.toString(); if (amount == null || amount.isEmpty()) { clearLabels(); } if (lastButtonPressed == null) return; calculateTip(lastButtonPressed); } @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { return; } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { return; } }); } @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, menu); return true; } // This method to any of the tip buttons being clicked. // It then determines which one was clicked and calculates the tip and total. public void calculateTip(View v) { lastButtonPressed = (Button)v; float tipPercent = 0.0f; switch(v.getId()) { case R.id.btnTenPercent: tipPercent = 0.1f; break; case R.id.btnFifteenPercent: tipPercent = 0.15f; break; case R.id.btnEighteenPercent: tipPercent = 0.18f; break; case R.id.btnTwentyPercent: tipPercent = 0.2f; break; } // Some simple validation. Make sure that the amount is not null or empty. String amount = etAmount.getText().toString(); if (amount == null || amount.isEmpty()) { // Show a toast to prompt the user to enter a transaction amount. Toast toast = Toast.makeText(this, R.string.empty_transaction_toaster, Toast.LENGTH_SHORT); toast.setGravity(Gravity.TOP | Gravity.CENTER, 0, 100); toast.show(); clearLabels(); return; } // Calculate and display the tip and total. float bill = Float.parseFloat(amount); float tip = bill * tipPercent; float total = bill + tip; tvTipAmount.setText(NumberFormat.getCurrencyInstance().format(tip) + " (" + lastButtonPressed.getText() + ")"); tvTotalAmount.setText(NumberFormat.getCurrencyInstance().format(total)); } // Clear out the labels which show the tip and total. private void clearLabels() { tvTipAmount.setText(R.string.empty_amount); tvTotalAmount.setText(R.string.empty_amount); } }