In: Computer Science
Application on Android Studio
"Tip Calculator" that calculates tip amount and total bill
amount
1. Takes as input a bill amount.
2. Allows user to select a tip percentage between 0-25%
3. Displays tip amount and total amount (total amount = bill amount+ tip amount)
Hint: As tip percentage is a fixed range, you can use a SEEKBAR and as user slides the bar, auto calculate the tip amount and total amount.
Android code for the simple tip calculator is given below
MainActivity.java file:
package com.example.tipcalculator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
    private EditText totalBillAmount;
    private SeekBar tipPercent;
    private TextView totalAmountToBePaid;
    private TextView totalAmountOfTipsToBePaid;
    private Button calculateTips;
    private int tipPercentValue = 0;
    private int tipsForNumberOfPeople = 0;
    private TextView tipPercentLabel;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_tip_calculator);
        totalBillAmount = (EditText)findViewById(R.id.bill_value);
        tipPercent = (SeekBar)findViewById(R.id.seekBar);
        totalAmountToBePaid = (TextView)findViewById(R.id.total_to_pay_result);
        totalAmountOfTipsToBePaid = (TextView)findViewById(R.id.total_tip_result);
        tipPercentLabel = (TextView)findViewById(R.id.tip_percent);
        tipPercent.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                tipPercentValue = progress/4;
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
            }
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                tipPercentLabel.setText("Tip Percent - " + seekBar.getProgress()/4);
            }
        });
        calculateTips = (Button) findViewById(R.id.calculate_tips);
        calculateTips.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (totalBillAmount.getText().toString().equals("") || totalBillAmount.getText().toString().isEmpty()) {
                    Toast.makeText(getApplicationContext(), "All Input field must be filled", Toast.LENGTH_LONG).show();
                    return;
                }
                double totalBillInput = Double.parseDouble(totalBillAmount.getText().toString());
                if (tipPercentValue == 0 ) {
                    Toast.makeText(getApplicationContext(), "Set values for Tip percent and split number", Toast.LENGTH_LONG).show();
                    return;
                }
                double percentageOfTip = (totalBillInput * tipPercentValue) / 100;
                double totalAmountForTheBill = totalBillInput + percentageOfTip;
                totalAmountToBePaid.setText(removeTrailingZero(String.valueOf(String.format("%.2f", totalAmountForTheBill))));
                totalAmountOfTipsToBePaid.setText(removeTrailingZero(String.valueOf(String.format("%.2f", percentageOfTip))));
            }
        });
        //return view;
    }
    public String removeTrailingZero(String formattingInput) {
        if (!formattingInput.contains(".")) {
            return formattingInput;
        }
        int dotPosition = formattingInput.indexOf(".");
        String newValue = formattingInput.substring(dotPosition, formattingInput.length());
        if (newValue.startsWith(".0")) {
            return formattingInput.substring(0, dotPosition);
        }
        return formattingInput;
    }
}
fragment_tip_calculator.xml file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
    <ScrollView
        android:id="@+id/scroll_content"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_above="@+id/calculate_tips"
        android:layout_marginBottom="6dp"
        android:background="#ffffff"
        android:padding="16dp"
        android:scrollbars="none">
        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
            <TextView
                android:id="@+id/first_binary"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentTop="true"
                android:layout_marginTop="89dp"
                android:text="total_bill"
                android:textStyle="bold"
                />
            <EditText
                android:id="@+id/bill_value"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_below="@+id/first_binary"
                android:layout_marginTop="4dp"
                android:lines="3"
                android:inputType="numberDecimal"
                android:padding="8dp" />
            <TextView
                android:id="@+id/tip_percent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_below="@+id/bill_value"
                android:text="tip_percent"
                android:textStyle="bold"
                />
            <SeekBar
                android:id="@+id/seekBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_below="@+id/tip_percent"/>
            <TextView
                android:id="@+id/total_to_pay"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/seekBar_one"
                android:layout_marginTop="354dp"
                android:text="total_amount_to_pay"
                android:textStyle="bold" />
            <TextView
                android:id="@+id/total_to_pay_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_below="@+id/total_to_pay"
                android:textStyle="bold"
                />
            <TextView
                android:id="@+id/total_tip"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="24dp"
                android:layout_below="@+id/total_to_pay_result"
                android:text="total_tip"
                android:textStyle="bold"
                />
            <TextView
                android:id="@+id/total_tip_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_below="@+id/total_tip"
                android:textStyle="bold"
                />
            <TextView
                android:id="@+id/tip_per_person_result"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="4dp"
                android:layout_below="@+id/tip_per_person"
                android:textStyle="bold"
                />
        </RelativeLayout>
    </ScrollView>
    <Button
        android:id="@+id/calculate_tips"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="calculate_tip"
        android:textAllCaps="false"
        android:padding="12dp"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="8dp"
        android:layout_alignParentBottom="true"/>
</RelativeLayout>
Screenshots:







Output Screenshots:


