Android UI How to - Create a Tip Calculator








The following code shows how to create a Tip Calculator. It shows

  • how to get value from EditText.
  • how to set value to TextView.
  • how to format a number.
  • how to catch exception and log it.




Example

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >/*from  w  ww . j  ava 2s.  co  m*/
    <EditText
        android:id="@+id/mealprice"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:autoText="true"
        />
    <Button
        android:id="@+id/calculate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Calculate Tip"
        />
    <TextView
        android:id="@+id/answer"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text=""
        />

</LinearLayout>

Java code

package com.java2s.app;
/*  ww w. jav  a2s  . c  o  m*/
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.text.NumberFormat;

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final EditText mealpricefield = (EditText) findViewById(R.id.mealprice);
        final TextView answerfield = (TextView) findViewById(R.id.answer);

        final Button button = (Button) findViewById(R.id.calculate);
        button.setOnClickListener(new Button.OnClickListener() {
            public void onClick(View v) {
                try {
                    // grab the meal price from the UI
                    String mealprice = mealpricefield.getText().toString();
                    String answer = "";
                    if (mealprice.indexOf("$") == -1) {
                        mealprice = "$" + mealprice;
                    }
                    float fmp = 0.0F;
                    NumberFormat nf = java.text.NumberFormat.getCurrencyInstance();
                    fmp = nf.parse(mealprice).floatValue();
                    //  20% tip
                    fmp *= 1.2;
                    answer = "Full Price, Including 20% Tip: " + nf.format(fmp);
                    answerfield.setText(answer);
                } catch (java.text.ParseException pe) {
                    Log.i("java2s.com", "Parse exception caught");
                    answerfield.setText("Failed to parse amount?");
                } catch (Exception e) {
                    Log.e("java2s.com", "Failed to Calculate Tip:" + e.getMessage());
                    e.printStackTrace();
                    answerfield.setText(e.getMessage());
                }
            }
        });
    }
}
null