Skip to content

Commit

Permalink
use a tag for each button, instead of separate method
Browse files Browse the repository at this point in the history
  • Loading branch information
eduaranega committed Jun 9, 2013
1 parent b7476de commit 8dcd227
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 20 deletions.
10 changes: 6 additions & 4 deletions res/layout/activity_tip_calculator.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="77dp"
android:onClick="calculateTip10"
android:onClick="calculateTip"
android:tag="10"
android:text="@string/ten_percentage" />

<Button
Expand All @@ -24,7 +25,8 @@
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button10"
android:layout_centerHorizontal="true"
android:onClick="calculateTip15"
android:onClick="calculateTip"
android:tag="15"
android:text="@string/fifteen_percentage" />

<Button
Expand All @@ -34,9 +36,9 @@
android:layout_alignBaseline="@+id/button15"
android:layout_alignBottom="@+id/button15"
android:layout_alignParentRight="true"
android:onClick="calculateTip20"
android:layout_marginRight="21dp"

android:onClick="calculateTip"
android:tag="20"
android:text="@string/twenty_percentage" />

<EditText
Expand Down
22 changes: 6 additions & 16 deletions src/com/android/eduardo/tipcalculator/TipCalculatorActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,21 @@ private boolean isEmpty(EditText etText) {
}
}

private void calculateTip(View view, double percentage) {
public void calculateTip(View view) {

double percentage = Double.parseDouble((String) view.getTag()) / 100;

EditText etAmount = (EditText)findViewById(R.id.editAmount);
TextView etTip = (TextView)findViewById(R.id.textTip);
TextView tvTip = (TextView)findViewById(R.id.textTip);

if (!isEmpty(etAmount)) {
double tip = percentage * Double.parseDouble(etAmount.getText().toString());
DecimalFormat df = new DecimalFormat("'$ '0.00");
etTip.setText(df.format(tip));
tvTip.setText(df.format(tip));
}
else {
etTip.setText("$ 0.00");
tvTip.setText("$ 0.00");
}
}

public void calculateTip10(View view) {
calculateTip(view,0.10);
}

public void calculateTip15(View view) {
calculateTip(view,0.15);
}

public void calculateTip20(View view) {
calculateTip(view,0.20);
}

}

0 comments on commit 8dcd227

Please sign in to comment.