From fb425e5fa7872c6086d6bec36bd0570d522175ba Mon Sep 17 00:00:00 2001 From: Jonry Simbolon Date: Thu, 10 Oct 2019 22:37:25 +0700 Subject: [PATCH] fix error when the first click operation button --- .../java/com/exuberant/calci/HomeActivity.java | 14 +++++++++----- app/src/main/res/layout/content_answer_view.xml | 6 ++---- 2 files changed, 11 insertions(+), 9 deletions(-) diff --git a/app/src/main/java/com/exuberant/calci/HomeActivity.java b/app/src/main/java/com/exuberant/calci/HomeActivity.java index 82471e3..c5b5797 100644 --- a/app/src/main/java/com/exuberant/calci/HomeActivity.java +++ b/app/src/main/java/com/exuberant/calci/HomeActivity.java @@ -11,7 +11,7 @@ public class HomeActivity extends AppCompatActivity implements View.OnClickListener { - private String currentNumber = "", totalCalculation = ""; + private String currentNumber = "0", totalCalculation = "0"; private TextView totalCalculationTextView, currentAnswerTextView; @Override @@ -56,8 +56,8 @@ public void onClick(View view) { //Handling clear button case R.id.btn_clear: - currentNumber = ""; - totalCalculation = ""; + currentNumber = "0"; + totalCalculation = "0"; break; //Handle calculation @@ -68,7 +68,11 @@ public void onClick(View view) { //Handle other numerical button clicks default: - currentNumber += button.getText().toString(); + if(!currentNumber.equals("0")){ + currentNumber += button.getText().toString(); + }else{ + currentNumber = button.getText().toString(); + } } updateDisplay(); } @@ -81,7 +85,7 @@ private void updateDisplay(){ private void handleOperatorClick(String operator){ if (!(currentNumber.equals("") || currentNumber.length() == 0)) { totalCalculation += currentNumber + operator; - currentNumber = ""; + currentNumber = "0"; } else { totalCalculation = totalCalculation.substring(0, totalCalculation.length() - 1); totalCalculation += operator; diff --git a/app/src/main/res/layout/content_answer_view.xml b/app/src/main/res/layout/content_answer_view.xml index cfeceec..faa7068 100644 --- a/app/src/main/res/layout/content_answer_view.xml +++ b/app/src/main/res/layout/content_answer_view.xml @@ -10,9 +10,8 @@ android:layout_marginStart="16dp" android:layout_marginTop="16dp" android:gravity="end" - android:textSize="24sp" + android:textSize="18sp" android:text="@string/total_calculation" - android:textColor="#757575" android:layout_width="match_parent" android:layout_height="wrap_content"/> @@ -23,9 +22,8 @@ android:layout_marginTop="8dp" android:layout_marginEnd="16dp" android:gravity="end" - android:textSize="32sp" + android:textSize="24sp" android:text="@string/current_calculation" - android:textColor="#757575" android:layout_width="match_parent" android:layout_height="wrap_content"/>