Buttons in MainActivity will now increment the counter.
This commit is contained in:
parent
754eda9c21
commit
03750feaf0
@ -4,6 +4,8 @@ import android.content.Intent;
|
|||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
|
import android.widget.TextView;
|
||||||
|
import android.widget.Toast;
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
|
|
||||||
@ -13,6 +15,10 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
protected CounterButtonSharedPreferenceHelper secondCounterButtonSharedPrefHelper;
|
protected CounterButtonSharedPreferenceHelper secondCounterButtonSharedPrefHelper;
|
||||||
protected CounterButtonSharedPreferenceHelper thirdCounterButtonSharedPrefHelper;
|
protected CounterButtonSharedPreferenceHelper thirdCounterButtonSharedPrefHelper;
|
||||||
|
|
||||||
|
protected TextView counter;
|
||||||
|
|
||||||
|
protected MaxCountSharedPreferenceHelper maxCountSharedPreferenceHelper;
|
||||||
|
|
||||||
protected Button settingsButton = null;
|
protected Button settingsButton = null;
|
||||||
|
|
||||||
protected Button firstCounterButton;
|
protected Button firstCounterButton;
|
||||||
@ -25,13 +31,18 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
setContentView(R.layout.activity_main);
|
setContentView(R.layout.activity_main);
|
||||||
|
|
||||||
settingsButton = findViewById(R.id.settings_button);
|
settingsButton = findViewById(R.id.settings_button);
|
||||||
|
|
||||||
firstCounterButton = findViewById(R.id.FirstEventButton);
|
firstCounterButton = findViewById(R.id.FirstEventButton);
|
||||||
secondCounterButton = findViewById(R.id.SecondEventButton);
|
secondCounterButton = findViewById(R.id.SecondEventButton);
|
||||||
thirdCounterButton = findViewById(R.id.ThirdEventButton);
|
thirdCounterButton = findViewById(R.id.ThirdEventButton);
|
||||||
|
|
||||||
|
counter = findViewById(R.id.counter);
|
||||||
|
|
||||||
firstCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.FIRST);
|
firstCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.FIRST);
|
||||||
secondCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.SECOND);
|
secondCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.SECOND);
|
||||||
thirdCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.THIRD);
|
thirdCounterButtonSharedPrefHelper = new CounterButtonSharedPreferenceHelper(MainActivity.this, CounterButton.THIRD);
|
||||||
|
maxCountSharedPreferenceHelper = new MaxCountSharedPreferenceHelper(MainActivity.this);
|
||||||
|
|
||||||
|
|
||||||
settingsButton.setOnClickListener(new View.OnClickListener() {
|
settingsButton.setOnClickListener(new View.OnClickListener() {
|
||||||
@Override
|
@Override
|
||||||
@ -40,6 +51,27 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
goToSettingsActivity();
|
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
|
@Override
|
||||||
@ -66,4 +98,20 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
|
Intent intent = new Intent(MainActivity.this, SettingsActivity.class);
|
||||||
startActivity(intent);
|
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();
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -71,6 +71,7 @@
|
|||||||
android:text="@string/total_count_text_view_string"/>
|
android:text="@string/total_count_text_view_string"/>
|
||||||
|
|
||||||
<TextView
|
<TextView
|
||||||
|
android:id="@+id/counter"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
style="@style/TotalCountTextSize"
|
style="@style/TotalCountTextSize"
|
||||||
|
Loading…
x
Reference in New Issue
Block a user