From 03750feaf0bd868712625b8260278a160666d0e9 Mon Sep 17 00:00:00 2001 From: minhtrannhat Date: Sat, 16 Sep 2023 15:31:47 -0400 Subject: [PATCH] Buttons in MainActivity will now increment the counter. --- .../com/example/assignment1/MainActivity.java | 48 +++++++++++++++++++ app/src/main/res/layout/activity_main.xml | 1 + 2 files changed, 49 insertions(+) diff --git a/app/src/main/java/com/example/assignment1/MainActivity.java b/app/src/main/java/com/example/assignment1/MainActivity.java index 1ef0c3f..292ecae 100644 --- a/app/src/main/java/com/example/assignment1/MainActivity.java +++ b/app/src/main/java/com/example/assignment1/MainActivity.java @@ -4,6 +4,8 @@ import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; +import android.widget.TextView; +import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; @@ -13,6 +15,10 @@ public class MainActivity extends AppCompatActivity { protected CounterButtonSharedPreferenceHelper secondCounterButtonSharedPrefHelper; protected CounterButtonSharedPreferenceHelper thirdCounterButtonSharedPrefHelper; + protected TextView counter; + + protected MaxCountSharedPreferenceHelper maxCountSharedPreferenceHelper; + protected Button settingsButton = null; protected Button firstCounterButton; @@ -25,13 +31,18 @@ public class MainActivity extends AppCompatActivity { setContentView(R.layout.activity_main); settingsButton = findViewById(R.id.settings_button); + firstCounterButton = findViewById(R.id.FirstEventButton); secondCounterButton = findViewById(R.id.SecondEventButton); thirdCounterButton = findViewById(R.id.ThirdEventButton); + counter = findViewById(R.id.counter); + firstCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.FIRST); secondCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.SECOND); thirdCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.THIRD); + maxCountSharedPreferenceHelper = new MaxCountSharedPreferenceHelper(MainActivity.this); + settingsButton.setOnClickListener(new View.OnClickListener() { @Override @@ -40,6 +51,27 @@ public class MainActivity extends AppCompatActivity { goToSettingsActivity(); } }); + + firstCounterButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + incrementCounter(); + } + }); + + secondCounterButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + incrementCounter(); + } + }); + + thirdCounterButton.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + incrementCounter(); + } + }); } @Override @@ -66,4 +98,20 @@ public class MainActivity extends AppCompatActivity { Intent intent = new Intent(MainActivity.this, SettingsActivity.class); startActivity(intent); } + + private void incrementCounter(){ + try{ + if (Integer.parseInt(counter.getText().toString()) < Integer.parseInt(maxCountSharedPreferenceHelper.getMaxCount())){ + String currentValue = counter.getText().toString(); + int intValue = Integer.parseInt(currentValue); + int newValue = intValue + 1; + counter.setText(String.valueOf(newValue)); + } + else { + Toast.makeText(getApplicationContext(), "Maximum count has been reached", Toast.LENGTH_SHORT).show(); + } + } catch(NumberFormatException e) { + Toast.makeText(getApplicationContext(), "Error incrementing the counter", Toast.LENGTH_SHORT).show(); + } + } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 4d4c55b..a361bf5 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -71,6 +71,7 @@ android:text="@string/total_count_text_view_string"/>