Initial import.

This commit is contained in:
Erik C. Thauvin 2008-11-04 08:39:33 +00:00
parent edeb77f683
commit 258d1ee693
17 changed files with 852 additions and 0 deletions

12
AndroidManifest.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="net.thauvin.erik.android.tiproid" android:versionCode="2" android:versionName="0.2">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:label="@string/app_name" android:name="Tiproid">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

BIN
res/drawable/calc.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.7 KiB

BIN
res/drawable/delete.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB

BIN
res/drawable/icon.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 8.6 KiB

12
res/layout/about.xml Normal file
View file

@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:gravity="center">
<TextView android:id="@+id/about_text_fld"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginTop="20dip" android:gravity="center"
android:autoLink="web|email" android:layout_marginBottom="20dip"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:text="@string/about_txt"></TextView>
</LinearLayout>

55
res/layout/calc.xml Normal file
View file

@ -0,0 +1,55 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<EditText android:layout_height="wrap_content"
android:layout_width="fill_parent" android:layout_marginLeft="20dip"
android:scrollHorizontally="true" android:autoText="false"
android:capitalize="none" android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginRight="20dip" android:numeric="decimal"
android:gravity="right" android:id="@+id/calc_edit_fld" />
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="20dip"
android:layout_marginRight="20dip">
<Button android:id="@+id/Button01" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="1"></Button>
<Button android:id="@+id/Button02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="2"></Button>
<Button android:id="@+id/Button03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="3"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginRight="20dip"
android:layout_marginLeft="20dip">
<Button android:id="@+id/Button04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="4"></Button>
<Button android:id="@+id/Button05" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="5"></Button>
<Button android:id="@+id/Button06" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="6"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="20dip">
<Button android:id="@+id/Button07" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="7"></Button>
<Button android:id="@+id/Button08" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="8"></Button>
<Button android:id="@+id/Button09" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="9"></Button>
</TableRow>
<TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginLeft="20dip">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ButtonDot"
android:text="."></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/Button0"
android:text="0"></Button>
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="@+id/ButtonBS"
android:src="@drawable/delete"></ImageButton>
</TableRow>
</TableLayout>
</LinearLayout>

77
res/layout/main.xml Normal file
View file

@ -0,0 +1,77 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_marginRight="20dip"
android:text="@string/main_tax_lbl_txt" android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/main_tax_lbl" android:layout_marginLeft="20dip" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_marginRight="20dip" android:layout_marginLeft="20dip">
<EditText android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:numeric="decimal"
android:gravity="right" android:id="@+id/main_tax_fld"
android:layout_width="wrap_content" android:minWidth="200dip" />
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/calc"
android:id="@+id/main_tax_edit_btn" android:minWidth="81dip"></ImageButton>
</LinearLayout>
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:layout_marginRight="20dip"
android:text="@string/main_bill_lbl_txt" android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/main_bill_lbl" android:layout_marginLeft="20dip" />
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:baselineAligned="false"
android:layout_marginRight="20dip" android:layout_marginLeft="20dip">
<EditText android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:numeric="decimal"
android:gravity="right" android:id="@+id/main_bill_fld"
android:layout_width="wrap_content" android:minWidth="200dip" />
<ImageButton android:layout_width="wrap_content"
android:layout_height="wrap_content" android:src="@drawable/calc"
android:id="@+id/main_bill_edit_btn" android:minWidth="81dip"></ImageButton>
</LinearLayout>
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TableRow android:id="@+id/TableRow01" android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/main_tip_lbl" android:text="@string/main_tip_lbl_txt"
android:minWidth="200dip" />
<TextView android:layout_height="wrap_content"
android:layout_width="wrap_content" android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/main_tip_lbl" android:text="@string/main_split_lbl_txt" />
</TableRow>
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_height="wrap_content">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:baselineAligned="false">
<Spinner android:layout_height="wrap_content" android:id="@+id/main_tip_spin"
android:layout_width="fill_parent"></Spinner>
</LinearLayout>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:baselineAligned="false">
<Spinner android:layout_height="wrap_content" android:id="@+id/main_split_spin"
android:layout_width="wrap_content"></Spinner>
</LinearLayout>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_height="wrap_content" android:layout_marginTop="20dip">
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/main_calc_btn_txt"
android:id="@+id/main_calculate_btn" style="?android:attr/buttonStyleSmall"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="@string/main_reset_btn_txt"
android:id="@+id/main_reset_btn" style="?android:attr/buttonStyleSmall"></Button>
</TableRow>
</TableLayout>
</LinearLayout>

42
res/layout/result.xml Normal file
View file

@ -0,0 +1,42 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical">
<TableLayout android:id="@+id/TableLayout01"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="20dip" android:layout_marginRight="20dip"
android:layout_marginTop="20dip">
<TableRow android:id="@+id/TableRow02" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium"
android:minEms="5" android:text="@string/result_tip_lbl_txt"
android:id="@+id/result_tip_lbl"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0.00"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right" android:id="@+id/result_tip_fld"></TextView>
</TableRow>
<TableRow android:id="@+id/TableRow03" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/result_total_lbl" android:text="@string/result_total_lbl_txt"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0.00"
android:textStyle="bold" android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right" android:id="@+id/result_total_fld"></TextView>
</TableRow>
<TableRow android:id="@+id/TableRow04" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_marginBottom="20dip">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/result_split_lbl" android:text="@string/result_split_lbl_txt"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0.00"
android:textAppearance="?android:attr/textAppearanceMedium"
android:gravity="right" android:id="@+id/result_split_fld"></TextView>
</TableRow>
</TableLayout>
</LinearLayout>

27
res/values/arrays.xml Normal file
View file

@ -0,0 +1,27 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="main_tip_array">
<item>5</item>
<item>10</item>
<item>12</item>
<item>15</item>
<item>18</item>
<item>20</item>
<item>25</item>
<item>30</item>
</string-array>
<string-array name="main_split_array">
<item>1</item>
<item>2</item>
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
<item>8</item>
<item>9</item>
<item>10</item>
<item>11</item>
<item>12</item>
</string-array>
</resources>

21
res/values/strings.xml Normal file
View file

@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">Tiproid</string>
<string name="about_txt">© 2008 Erik C. Thauvin\nAll Rights Reserved\n\nerik@thauvin.net\n\nhttp://mobile.thauvin.net</string>
<string name="about_menu_txt">About</string>
<string name="alert_dialog_ok">OK</string>
<string name="alert_dialog_cancel">Cancel</string>
<string name="main_bill_lbl_txt">Bill Total Amount:</string>
<string name="main_calc_btn_txt">Calculate</string>
<string name="main_reset_btn_txt">Reset</string>
<string name="main_tax_lbl_txt">Tax Amount:</string>
<string name="main_tax_hint_land_txt">Use the keyboard...</string>
<string name="main_tax_hint_txt">Tap button to edit...</string>
<string name="main_tip_lbl_txt">Tip %:</string>
<string name="main_split_lbl_txt">Split:</string>
<string name="result_tip_lbl_txt">Tip ?%:</string>
<string name="result_split_lbl_txt">Split x ?:</string>
<string name="result_total_lbl_txt">Total:</string>
<string name="error_bill_txt">Please enter the bill amount...</string>
<string name="sample_txt">Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.</string>
</resources>

BIN
screenshots/1.psd Normal file

Binary file not shown.

BIN
screenshots/2.psd Normal file

Binary file not shown.

BIN
screenshots/3.psd Normal file

Binary file not shown.

BIN
screenshots/tiproid.gif Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 367 KiB

10
sign.bat Normal file
View file

@ -0,0 +1,10 @@
@echo off
"%JAVA_HOME%\bin\jarsigner" -verbose -keystore "%USERPROFILE%\personal\android.keystore" "%1" android
if errorlevel 1 goto ERROR
"%JAVA_HOME%\bin\jarsigner" -verify "%1"
if errorlevel 1 goto ERROR
goto DONE
:ERROR
@pause
:DONE
@echo on

View file

@ -0,0 +1,86 @@
/* AUTO-GENERATED FILE. DO NOT MODIFY.
*
* This class was automatically generated by the
* aapt tool from the resource data it found. It
* should not be modified by hand.
*/
package net.thauvin.erik.android.tiproid;
public final class R {
public static final class array {
public static final int main_split_array=0x7f040001;
public static final int main_tip_array=0x7f040000;
}
public static final class attr {
}
public static final class drawable {
public static final int calc=0x7f020000;
public static final int delete=0x7f020001;
public static final int icon=0x7f020002;
}
public static final class id {
public static final int Button0=0x7f060011;
public static final int Button01=0x7f060004;
public static final int Button02=0x7f060005;
public static final int Button03=0x7f060006;
public static final int Button04=0x7f060008;
public static final int Button05=0x7f060009;
public static final int Button06=0x7f06000a;
public static final int Button07=0x7f06000c;
public static final int Button08=0x7f06000d;
public static final int Button09=0x7f06000e;
public static final int ButtonBS=0x7f060012;
public static final int ButtonDot=0x7f060010;
public static final int TableLayout01=0x7f060002;
public static final int TableRow01=0x7f060003;
public static final int TableRow02=0x7f060007;
public static final int TableRow03=0x7f06000b;
public static final int TableRow04=0x7f06000f;
public static final int about_text_fld=0x7f060000;
public static final int calc_edit_fld=0x7f060001;
public static final int main_bill_edit_btn=0x7f060018;
public static final int main_bill_fld=0x7f060017;
public static final int main_bill_lbl=0x7f060016;
public static final int main_calculate_btn=0x7f06001c;
public static final int main_reset_btn=0x7f06001d;
public static final int main_split_spin=0x7f06001b;
public static final int main_tax_edit_btn=0x7f060015;
public static final int main_tax_fld=0x7f060014;
public static final int main_tax_lbl=0x7f060013;
public static final int main_tip_lbl=0x7f060019;
public static final int main_tip_spin=0x7f06001a;
public static final int result_split_fld=0x7f060023;
public static final int result_split_lbl=0x7f060022;
public static final int result_tip_fld=0x7f06001f;
public static final int result_tip_lbl=0x7f06001e;
public static final int result_total_fld=0x7f060021;
public static final int result_total_lbl=0x7f060020;
}
public static final class layout {
public static final int about=0x7f030000;
public static final int calc=0x7f030001;
public static final int main=0x7f030002;
public static final int result=0x7f030003;
}
public static final class string {
public static final int about_menu_txt=0x7f050002;
public static final int about_txt=0x7f050001;
public static final int alert_dialog_cancel=0x7f050004;
public static final int alert_dialog_ok=0x7f050003;
public static final int app_name=0x7f050000;
public static final int error_bill_txt=0x7f050010;
public static final int main_bill_lbl_txt=0x7f050005;
public static final int main_calc_btn_txt=0x7f050006;
public static final int main_reset_btn_txt=0x7f050007;
public static final int main_split_lbl_txt=0x7f05000c;
public static final int main_tax_hint_land_txt=0x7f050009;
public static final int main_tax_hint_txt=0x7f05000a;
public static final int main_tax_lbl_txt=0x7f050008;
public static final int main_tip_lbl_txt=0x7f05000b;
public static final int result_split_lbl_txt=0x7f05000e;
public static final int result_tip_lbl_txt=0x7f05000d;
public static final int result_total_lbl_txt=0x7f05000f;
public static final int sample_txt=0x7f050011;
}
}

View file

@ -0,0 +1,510 @@
/*
* @(#)Tiproid.java
*
* Copyright (c) 2008, Erik C. Thauvin (http://erik.thauvin.net/)
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
* met:
*
* Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* Neither the name of the authors nor the names of its contributors may be
* used to endorse or promote products derived from this software without
* specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id$
*
*/
package net.thauvin.erik.android.tiproid;
/**
* <code>Tiproid</code> is a simple tip calculator for Android.
*
* @author <a href="mailto:erik@thauvin.net">Erik C. Thauvin</a>
* @version $Revision$, $Date$
* @created Oct 9, 2008
* @since 1.0
*/
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class Tiproid extends Activity
{
public static final String PREFS_TIP_RATE = "TipRate";
/**
* Displays the calc dialog.
*
* @param editFld The edit field.
*/
private void calcDialog(final EditText editFld)
{
final LayoutInflater factory = LayoutInflater.from(this);
final View textEntryView = factory.inflate(R.layout.calc, null);
final Button zero = (Button) textEntryView.findViewById(R.id.Button0);
final Button one = (Button) textEntryView.findViewById(R.id.Button01);
final Button two = (Button) textEntryView.findViewById(R.id.Button02);
final Button three = (Button) textEntryView.findViewById(R.id.Button03);
final Button four = (Button) textEntryView.findViewById(R.id.Button04);
final Button five = (Button) textEntryView.findViewById(R.id.Button05);
final Button six = (Button) textEntryView.findViewById(R.id.Button06);
final Button seven = (Button) textEntryView.findViewById(R.id.Button07);
final Button eight = (Button) textEntryView.findViewById(R.id.Button08);
final Button nine = (Button) textEntryView.findViewById(R.id.Button09);
final Button dot = (Button) textEntryView.findViewById(R.id.ButtonDot);
final ImageButton bs = (ImageButton) textEntryView.findViewById(R.id.ButtonBS);
final String curText = editFld.getText().toString();
final EditText calcEdit = (EditText) textEntryView.findViewById(R.id.calc_edit_fld);
if (!TextUtils.isEmpty(curText))
{
calcEdit.setText(curText);
}
zero.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("0");
}
});
one.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("1");
}
});
two.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("2");
}
});
three.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("3");
}
});
four.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("4");
}
});
five.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("5");
}
});
six.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("6");
}
});
seven.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("7");
}
});
eight.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("8");
}
});
nine.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append("9");
}
});
dot.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcEdit.append(".");
}
});
bs.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
final String txt = calcEdit.getText().toString();
if (!TextUtils.isEmpty(txt))
{
if (txt.length() > 0)
{
calcEdit.setText(txt.subSequence(0, txt.length() - 1));
calcEdit.setSelection(txt.length() - 1);
}
}
}
});
new AlertDialog.Builder(this).setView(textEntryView).setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
editFld.setText(calcEdit.getText().toString());
editFld.requestFocus();
}
}).setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
editFld.requestFocus();
}
}).show();
}
/**
* Calculates the tip and displays the result dialog.
*
* @param billTxt The bill amount.
* @param taxTxt The tax amount.
* @param tipTxt The tip rate.
* @param splitTx The split count.
*/
private void calculate(String billTxt, String taxTxt, String tipTxt, String splitTxt)
{
if (TextUtils.isEmpty(billTxt))
{
Toast.makeText(Tiproid.this, R.string.error_bill_txt, Toast.LENGTH_SHORT).show();
}
else
{
final int taxTotal;
if (TextUtils.isEmpty(taxTxt))
{
taxTotal = 0;
}
else
{
taxTotal = parseInt(taxTxt);
}
final int subTotal = parseInt(billTxt) - taxTotal;
final int tipRate = Integer.parseInt(tipTxt);
final int splitCount = Integer.parseInt(splitTxt);
int tipTotal = (subTotal * tipRate) / 100;
int billTotal = subTotal + tipTotal + taxTotal;
if ((billTotal % 100) < 50)
{
while ((billTotal % 100) != 0)
{
tipTotal--;
billTotal = subTotal + tipTotal + taxTotal;
}
}
else
{
while ((billTotal % 100) != 0)
{
tipTotal++;
billTotal = subTotal + tipTotal + taxTotal;
}
}
final LayoutInflater factory = LayoutInflater.from(this);
final View resultView = factory.inflate(R.layout.result, null);
final TextView tipFld = (TextView) resultView.findViewById(R.id.result_tip_fld);
final TextView totalFld = (TextView) resultView.findViewById(R.id.result_total_fld);
final TextView tipLbl = (TextView) resultView.findViewById(R.id.result_tip_lbl);
final TextView splitFld = (TextView) resultView.findViewById(R.id.result_split_fld);
final TextView splitLbl = (TextView) resultView.findViewById(R.id.result_split_lbl);
tipLbl.setText(tipLbl.getText().toString().replace("?", tipTxt));
tipFld.setText(parseStr(tipTotal));
totalFld.setText(parseStr(billTotal));
if (splitCount == 1)
{
splitFld.setText("");
splitLbl.setText("");
}
else
{
splitLbl.setText(splitLbl.getText().toString().replace("?", splitTxt));
int split = billTotal / splitCount;
if ((split * splitCount) < billTotal)
{
split += 1;
}
splitFld.setText(parseStr(split));
}
new AlertDialog.Builder(this).setView(resultView).setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// do nothing
}
}).show();
}
}
/**
* Returns the current version number.
*
* @return The current version number or empty.
*/
private String getVersionNumber()
{
try
{
return getPackageManager().getPackageInfo(getPackageName(), 0).versionName;
}
catch (final NameNotFoundException e)
{
return "";
}
};
/**
* Initializes the various controls.
*/
private void init()
{
final EditText billFld = (EditText) findViewById(R.id.main_bill_fld);
final EditText taxFld = (EditText) findViewById(R.id.main_tax_fld);
final Spinner tipSpin = (Spinner) findViewById(R.id.main_tip_spin);
final Spinner splitSpin = (Spinner) findViewById(R.id.main_split_spin);
final Button calcBtn = (Button) findViewById(R.id.main_calculate_btn);
final Button resetBtn = (Button) findViewById(R.id.main_reset_btn);
final ImageButton billEditBtn = (ImageButton) findViewById(R.id.main_bill_edit_btn);
final ImageButton taxEditBtn = (ImageButton) findViewById(R.id.main_tax_edit_btn);
// Disable edit buttons in landscape mode
final boolean enableBtns = getWindow().getWindowManager().getDefaultDisplay().getWidth() < getWindow().getWindowManager()
.getDefaultDisplay().getHeight();
billEditBtn.setEnabled(enableBtns);
taxEditBtn.setEnabled(enableBtns);
if (!enableBtns)
{
taxFld.setHint(getString(R.string.main_tax_hint_land_txt));
}
else
{
taxFld.setHint(getString(R.string.main_tax_hint_txt));
}
final ArrayAdapter<CharSequence> tipAdapter = ArrayAdapter.createFromResource(this, R.array.main_tip_array,
android.R.layout.simple_spinner_item);
final int defaultTipRate = (tipAdapter.getCount() / 2) - 1;
tipAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
tipSpin.setAdapter(tipAdapter);
final ArrayAdapter<CharSequence> splitAdapter = ArrayAdapter.createFromResource(this, R.array.main_split_array,
android.R.layout.simple_spinner_item);
splitAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
splitSpin.setAdapter(splitAdapter);
final SharedPreferences settings = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
tipSpin.setSelection(settings.getInt(PREFS_TIP_RATE, defaultTipRate));
calcBtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calculate(billFld.getText().toString(), taxFld.getText().toString(), tipSpin.getSelectedItem().toString(), splitSpin
.getSelectedItem().toString());
}
});
resetBtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
billFld.setText("");
taxFld.setText("");
splitSpin.setSelection(0);
tipSpin.setSelection(settings.getInt(PREFS_TIP_RATE, defaultTipRate));
}
});
billEditBtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcDialog(billFld);
}
});
taxEditBtn.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View view)
{
calcDialog(taxFld);
}
});
}
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
init();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add(0, Menu.FIRST, 0, R.string.about_menu_txt).setIcon(android.R.drawable.ic_menu_info_details);
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
if (item.getItemId() == Menu.FIRST)
{
final LayoutInflater factory = LayoutInflater.from(this);
final View aboutView = factory.inflate(R.layout.about, null);
new AlertDialog.Builder(this).setView(aboutView).setIcon(android.R.drawable.ic_dialog_info).setTitle(
getString(R.string.app_name) + ' ' + getVersionNumber()).setPositiveButton(R.string.alert_dialog_ok,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// do nothing
}
}).show();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onStop()
{
super.onStop();
final SharedPreferences settings = getSharedPreferences(getString(R.string.app_name), Context.MODE_PRIVATE);
final SharedPreferences.Editor editor = settings.edit();
final Spinner tipSpin = (Spinner) findViewById(R.id.main_tip_spin);
editor.putInt(PREFS_TIP_RATE, tipSpin.getSelectedItemPosition());
editor.commit();
}
/**
* Parses a given string to an integer, the decimal point is removed.
*
* @param str The string to parse.
* @return The parsed integer.
*/
private int parseInt(String str)
{
final int dec = str.lastIndexOf('.');
final int len = str.length();
if (dec == -1)
{
return Integer.parseInt(str + "00");
}
else if (dec == (len - 1))
{
return Integer.parseInt(str.substring(0, dec) + "00");
}
else if (dec == (len - 2))
{
return Integer.parseInt(str.substring(0, dec) + str.substring(dec + 1) + '0');
}
return Integer.parseInt(str.substring(0, dec) + str.substring(dec + 1, dec + 3));
}
/**
* Parses the given integer into to a string, the decimal point is added.
*
* @param i The integer.
* @return The parsed string.
*/
private String parseStr(int i)
{
final String s = String.valueOf(i);
return s.substring(0, s.length() - 2) + '.' + s.substring(s.length() - 2);
}
}