title: Bill Splitter Calculator Android App
body: <p>I'm trying to create an Android app to help to calculate split the bill if let's say you're eating out in a party of 2 people or more. </p>
  
  <p>You're supposed to enter the subtotal of the bill, enter the number of people in the party, enter applicable discount if any, there are 2 checkboxes for 7% tax, and 10% service charges if it hasn't been included in the bill yet. Finally you just need to click on the "calculate button" for the app to calculate how much each person has to pay.</p>
  
  <p>My Questions are: </p>
  
  <ol>
  <li>for subtotal, it's supposed to be double instead of int, but I'm not sure how to parse String into a double. Is there a way to do this?</li>
  <li>I'm not sure if that is the best way to activate the Checkboxes for the tax and 10% tips</li>
  <li>When I click on the calculate button, it is supposed to display the Toast message with the result of the calculation, but nothing appears. I'm not sure if the problem is with parseInteger, checkBoxes, or if the onClick method is wrong, or all of them.</li>
  </ol>
  
  <p>Here's the code that I wrote:</p>
  
  <pre><code>    package com.kevinw.BillSplitter;
  
  import android.app.Activity;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.CheckBox;
  import android.widget.CompoundButton;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class BillSplitter extends Activity implements OnClickListener {
  /** Declares XML Widgets */
  private EditText numberDiners;
  private EditText enterAmount;
  private EditText enterDiscount;
  private CheckBox gst;
  private CheckBox tips;
  private CheckBox cess;
  double result;
  private Button calculate;
  private TextView resultAmount;
  
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
  
  
  //Initialize Widgets
  numberDiners = (EditText) findViewById(R.id.numberDiners);
  enterAmount = (EditText) findViewById(R.id.EnterAmount);
  enterDiscount = (EditText) findViewById(R.id.EnterDiscount);
  calculate = (Button) findViewById(R.id.calculate);   
  
  //Initialize CheckBoxes
  gst = (CheckBox) findViewById(R.id.cbCheck1);
  gst.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { 
  public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
      if (gst.isChecked()) {
          result = result + (0.07 * result);
  }
  else {
      result = result;
          }
      }
  });
  
  
  tips = (CheckBox) findViewById(R.id.cbCheck2);
  tips.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
      public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
          if (tips.isChecked()) {
              result = result + (0.1 * result);       
          }
          else {
              result = result;
          }   
      }
  });
  
  }  
  
  
  @Override
  public void onClick(View v) {
  
  //Initialize EditTexts
  String amount = enterAmount.getText().toString();
  int subtotal = Integer.parseInt(amount);
  
  String diners = numberDiners.getText().toString();
  int people = Integer.parseInt(diners);
  
  String disc = enterDiscount.getText().toString();
  int discount = Integer.parseInt(disc);
  double discounted = discount / 100;
  result = (1 - discounted) * (subtotal / people);    
  
  switch (v.getId()) {
  case(R.id.calculate):
      Toast.makeText(this, "The Amount a Person has to pay: $" + result,     Toast.LENGTH_LONG).show();
  break;
      }
  }
  }    
  </code></pre>
  
  <p>and if it helps, this is the XML code for the layout:</p>
  
  <pre><code>    &lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"&gt;
  &lt;TextView
      android:id="@+id/dinersView"  
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/hello"/&gt;
  &lt;TextView
      android:id="@+id/Enter"  
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/dinersView" 
      android:layout_alignLeft="@+id/EnterAmount"
      android:text="@string/enter"/&gt;
  &lt;EditText
      android:id="@+id/numberDiners"
      android:layout_height="wrap_content"
      android:layout_below="@+id/dinersView"
      android:layout_width="100dip"/&gt;
  &lt;EditText
      android:id="@+id/EnterAmount"  
      android:layout_height="wrap_content"
      android:layout_below="@+id/Enter"
      android:layout_toRightOf="@+id/numberDiners" 
      android:layout_width="220dip"/&gt;
  &lt;TextView
      android:id="@+id/Discount"  
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_below="@+id/EnterAmount" 
      android:layout_alignLeft="@+id/EnterAmount"
      android:text="@string/discount"/&gt;
  &lt;EditText
      android:id="@+id/EnterDiscount"  
      android:layout_height="wrap_content"
      android:layout_below="@+id/Discount"
      android:layout_alignLeft="@+id/Discount"
      android:layout_width="220dip"/&gt;
  &lt;CheckBox
      android:id="@+id/cbCheck1"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_below="@+id/EnterDiscount" /&gt;
  &lt;CheckBox
      android:id="@+id/cbCheck2"
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_below="@+id/cbCheck1" /&gt; 
  &lt;TextView
      android:id="@+id/gst"  
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/cbCheck1" 
      android:layout_alignTop="@+id/cbCheck1"
      android:layout_alignLeft="@+id/enterDiscount"   
      android:layout_marginTop="10dip"
      android:text="@string/GST"/&gt;    
  &lt;TextView
      android:id="@+id/tips"  
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content"
      android:layout_toRightOf="@+id/cbCheck2" 
      android:layout_alignTop="@+id/cbCheck2"
      android:layout_marginTop="10dip"
      android:text="@string/tips"/&gt;
  &lt;Button 
      android:id="@+id/calculate"
      android:layout_below="@+id/cbCheck2"
      android:layout_width="wrap_content" 
      android:text="@string/calculate" 
      android:layout_height="wrap_content" 
      android:layout_alignParentLeft="true"/&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Thank you for all of the help. Really appreciate it.</p>