Buttons in MainActivity will now increment the counter.

This commit is contained in:
minhtrannhat 2023-09-16 15:31:47 -04:00
parent 754eda9c21
commit 03750feaf0
Signed by: minhtrannhat
GPG Key ID: E13CFA85C53F8062
2 changed files with 49 additions and 0 deletions

View File

@ -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();
}
}
}

View File

@ -71,6 +71,7 @@
android:text="@string/total_count_text_view_string"/>
<TextView
android:id="@+id/counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="@style/TotalCountTextSize"