I wonder if anyone can help me. I'm trying to learn programming/Java by building a quiz app.
I have multiple categories. Each category can score points and save using sharedpreferences. Points are collected and added to a highest score variable, again using sharedpreferences.
What I want to do is every time I want to restart a particular category, I want that category's saved points to be subtracted/removed from the highest score. So, for example, if the highest score is currently at 160 and a category final score was 60, when I restart that category, the highest score should now display 100.
1: What's currently working is, after finishing and replaying a single category, the category score is deleted/removed from the highest score.
2: But then, if I finish more than one category and replay all of them, only one category score will be resetted.
3: And again, if I finish more than one category and replay the second category, all category scores will be removed from highest score.
public class CategoriesActivity extends AppCompatActivity {
public static final String CATEGORY_ID = "category_Id";
public Button catOne_Button_1, catTwo_Button_2, catThree_Button_3, catFour_Button_4, backToFrontEnd;
MediaPlayer categorySelectScreen, button1Press, button2Press, button3Press;
Animation buttonBlink;
TextView levelUnlockText;
long backPressedTime;
public ScrollView scrollView;
PopupWindow congratulationsPopup;
LayoutInflater layoutInflater;
ConstraintLayout constraintLayout;
YoYo.YoYoString customAnimation;
int catOneScore;
int catTwoScore;
int subtractScore1;
int subtractScore2;
int subtractScore3;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_category);
this.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
categorySelectScreen = MediaPlayer.create(this, R.raw.choose_your_destiny_mk4_music);
categorySelectScreen.start();
scrollView = findViewById(R.id.scroll_view);
levelUnlockText = findViewById(R.id.unlock_text);
YoYo.with(Techniques.Pulse)
.duration(500)
.repeat(20)
.playOn(findViewById(R.id.unlock_text));
//Typeface typeface = getResources().getFont(R.font.sniglet_extrabold);
Typeface typeface = ResourcesCompat.getFont(this, R.font.sniglet_extrabold);
levelUnlockText.setTypeface(typeface);
catOne_Button_1 = findViewById(R.id.button_Switch1);
catTwo_Button_2 = findViewById(R.id.button_Switch2);
catThree_Button_3 = findViewById(R.id.button_Switch3);
catFour_Button_4 = findViewById(R.id.button_Switch4);
//Added sounds to each category when pressed
button1Press = MediaPlayer.create(this, R.raw.excellent_mk3);
button2Press = MediaPlayer.create(this, R.raw.trying_to_win_mk3);
buttonBlink = AnimationUtils.loadAnimation(this, R.anim.blink_anim);
backToFrontEnd = findViewById(R.id.backToFrontEnd);
backToFrontEnd.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
categorySelectScreen.stop();
Intent intent = new Intent(CategoriesActivity.this, MainFrontEnd.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finishAffinity();
}
});
unlockCategories();
catOne_Button_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
catOne_Button_1.startAnimation(buttonBlink);
if (!catOne_Button_1.getText().equals("COMPLETED!")) {
doubleButtonTabDisabled();
button1Press.start();
categorySelectScreen.release();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 1);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1500);
}
if (catOne_Button_1.getText().equals("COMPLETED!")) {
new AlertDialog.Builder(CategoriesActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("PLAY CAT_ONE AGAIN?")
.setMessage("Category score and questions will be resetted")
.setPositiveButton("Replay", (dialog, which) -> {
doubleButtonTabDisabled();
button1Press.start();
categorySelectScreen.release();
catOne_Button_1.setText("LET'S GO!");
catOne_Button_1.setTextColor(Color.YELLOW);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("CATEGORY_1_SCORE");
editor.apply();
PreferencesConfig.highestScores(getApplicationContext(), new CongratsScreen().highestScore - new CongratsScreen().categoryNumber);
SharedPreferences preferences1 = getSharedPreferences(PreferencesConfig.SAVE_GAME_POSITION, MODE_PRIVATE);
SharedPreferences.Editor editor1 = preferences1.edit();
editor1.remove(PreferencesConfig.CAT_ONE_CURRENT_SAVED_POINTS);
editor1.remove(PreferencesConfig.CAT_ONE_CURRENT_SAVED_QUESTION);
editor1.remove(PreferencesConfig.CAT_ONE_SAVED_QUESTION_NUMBER);
editor1.apply();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 1);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1500);
})
.setNegativeButton("Cancel", null)
.show();
catOne_Button_1.setEnabled(true);
}
}
});
catTwo_Button_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
catTwo_Button_2.startAnimation(buttonBlink);
if (!catTwo_Button_2.getText().equals("COMPLETED!")) {
doubleButtonTabDisabled();
button2Press.start();
categorySelectScreen.release();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 2);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1800);
}
if (catTwo_Button_2.getText().equals("COMPLETED!")) {
new AlertDialog.Builder(CategoriesActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("PLAY CAT_TWO AGAIN?")
.setMessage("Category score and questions will be resetted")
.setPositiveButton("Replay", (dialog, which) -> {
doubleButtonTabDisabled();
button2Press.start();
categorySelectScreen.release();
catTwo_Button_2.setText("LET'S GO!");
catTwo_Button_2.setTextColor(Color.YELLOW);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("CATEGORY_2_SCORE");
//editor.remove(PreferencesConfig.HIGHEST_SCORE);
editor.apply();
PreferencesConfig.highestScores(getApplicationContext(), new CongratsScreen().cat2);
SharedPreferences preferences1 = getSharedPreferences(PreferencesConfig.SAVE_GAME_POSITION, MODE_PRIVATE);
SharedPreferences.Editor editor1 = preferences1.edit();
editor1.remove(PreferencesConfig.CAT_TWO_CURRENT_SAVED_POINTS);
editor1.remove(PreferencesConfig.CAT_TWO_CURRENT_SAVED_QUESTION);
editor1.remove(PreferencesConfig.CAT_TWO_SAVED_QUESTION_NUMBER);
editor1.apply();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 2);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1500);
})
.setNegativeButton("Cancel", null)
.show();
catTwo_Button_2.setEnabled(true);
}
}
});
catThree_Button_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
catThree_Button_3.startAnimation(buttonBlink);
if (!catThree_Button_3.getText().equals("COMPLETED!")) {
doubleButtonTabDisabled();
//button1Press.start();
categorySelectScreen.release();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 3);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1500);
}
if (catThree_Button_3.getText().equals("COMPLETED!")) {
new AlertDialog.Builder(CategoriesActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("PLAY CAT_THREE AGAIN?")
.setMessage("Category score and questions will be resetted")
.setPositiveButton("Replay", (dialog, which) -> {
doubleButtonTabDisabled();
//button1Press.start();
categorySelectScreen.release();
catThree_Button_3.setText("LET'S GO!");
catThree_Button_3.setTextColor(Color.YELLOW);
SharedPreferences preferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
SharedPreferences.Editor editor = preferences.edit();
editor.remove("CATEGORY_3_SCORE");
editor.apply();
PreferencesConfig.highestScores(getApplicationContext(), new CongratsScreen().highestScore);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button1Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 3);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 1500);
})
.setNegativeButton("Cancel", null)
.show();
catThree_Button_3.setEnabled(true);
}
}
});
catFour_Button_4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
doubleButtonTabDisabled();
catFour_Button_4.startAnimation(buttonBlink);
//button4Press.start();
categorySelectScreen.release();
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
//button2Press.release();
Intent intent = new Intent(CategoriesActivity.this, MainActivity.class);
intent.putExtra(CATEGORY_ID, 4);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
}
}, 2000);
}
});
}
public void doubleButtonTabDisabled() {
catOne_Button_1.setEnabled(false);
catTwo_Button_2.setEnabled(false);
catThree_Button_3.setEnabled(false);
catFour_Button_4.setEnabled(false);
}
private void unlockCategories() {
SharedPreferences preferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
catOneScore = preferences.getInt("CATEGORY_1_SCORE", 0);
catTwoScore = preferences.getInt("CATEGORY_2_SCORE", 0);
if (catOneScore <= new MainActivity().pointsGoal && catTwoScore <= new MainActivity().pointsGoal) {
catThree_Button_3.setTextColor(Color.RED);
}
else {
if (catOneScore >= new MainActivity().pointsGoal) {
catOne_Button_1.setTextColor(Color.GRAY);
catOne_Button_1.setText("COMPLETED!");
}
if (catTwoScore >= new MainActivity().pointsGoal) {
catTwo_Button_2.setTextColor(Color.GRAY);
catTwo_Button_2.setText("COMPLETED!");
}
shakeButton(catThree_Button_3);
levelUnlockText.setText("80s UNLOCKED");
levelUnlockText.setTextColor(Color.GREEN);
}
unlockCategory(preferences, "CATEGORY_3_SCORE", "90s UNLOCKED", catThree_Button_3, catFour_Button_4, new MainActivity().pointsGoal);
unlockCategory(preferences, "CATEGORY_4_SCORE", "2000s UNLOCKED", catFour_Button_4, catFour_Button_4, new MainActivity().pointsGoal);
}
private void unlockCategory(SharedPreferences preferences, String category_Id, String unlockMessage,
Button previousCategoryButton, Button currentCategoryButton, int unlockScore) {
int categoryScore = preferences.getInt(category_Id, 0);
if (categoryScore <= unlockScore) {
//currentCategoryButton.setEnabled(false);
currentCategoryButton.setTextColor(Color.RED);
}
else {
//previousCategoryButton.setEnabled(false);
previousCategoryButton.setTextColor(Color.GRAY);
previousCategoryButton.setText("COMPLETED!");
currentCategoryButton.setEnabled(true);
currentCategoryButton.setTextColor(Color.YELLOW);
shakeButton(currentCategoryButton);
animateUnlockText(unlockMessage);
}
}
private void shakeButton(Button seventies_button_3) {
if (customAnimation != null && customAnimation.isRunning()) {
customAnimation.stop();
}
customAnimation = YoYo.with(Techniques.Shake)
.duration(500)
.repeat(20)
.pivot(YoYo.CENTER_PIVOT, YoYo.CENTER_PIVOT)
.interpolate(new AccelerateDecelerateInterpolator())
.playOn(seventies_button_3);
}
private void animateUnlockText(String unlockText) {
YoYo.with(Techniques.Pulse)
.duration(500)
.repeat(20)
.playOn(findViewById(R.id.unlock_text));
levelUnlockText.setText(unlockText);
levelUnlockText.setTextColor(Color.GREEN);
}
@Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
categorySelectScreen.stop();
Intent intentBackToCategory = new Intent(CategoriesActivity.this, MainFrontEnd.class);
startActivity(intentBackToCategory);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finishAffinity();
}
backPressedTime = System.currentTimeMillis();
}
public void congratulationsPopop() {
constraintLayout = findViewById(R.id.rootLayout);
layoutInflater = (LayoutInflater) getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
ViewGroup container = (ViewGroup) layoutInflater.inflate(R.layout.activity_options, null);
congratulationsPopup = new PopupWindow(container, 900, 1400, true);
congratulationsPopup.showAtLocation(constraintLayout, Gravity.CENTER, 0, 0);
}
}
It's not the prettiest code, but I'm trying.
CongratsScreen is where the final result is displayed.
I hope this all sort of makes sense.
I appreciate any help I can get.
Thank you.
public class CongratsScreen extends AppCompatActivity {
TextView textViewFinalScore;
TextView textViewHiScore;
public static int highestScore;
public static int cat1, cat2, cat3;
public int categoryScore, categoryScore1;
public int finalScore;
public int categoryNumber;
MediaPlayer congrats;
Button backToMainScreen;
Animation fadeInButton, blinkingButton;
private VideoView videoView;
long backPressedTime;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_congrats_screen);
this.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
videoView = findViewById(R.id.videoView3);
Uri uri = Uri.parse("android.resource://" +getPackageName()+ "/" +R.raw.circular_stars_trail_moving_1080p);
videoView.setVideoURI(uri);
videoView.start();
textViewFinalScore = findViewById(R.id.final_game_score);
textViewHiScore = findViewById(R.id.current_highest_score);
Intent intent = getIntent();
finalScore = intent.getIntExtra("FINAL_SCORE", 0);
categoryNumber = intent.getIntExtra(CategoriesActivity.CATEGORY_ID, 0);
textViewFinalScore.setText("You Scored: " +finalScore+" Points");
SharedPreferences preferences = getSharedPreferences("SHARED_PREFS", MODE_PRIVATE);
highestScore = preferences.getInt(PreferencesConfig.HIGHEST_SCORE, 0);
categoryScore = finalScore;
finalScore = finalScore + highestScore;
textViewHiScore.setText("HIGHEST SCORE: "+finalScore);
SharedPreferences.Editor editor = preferences.edit();
editor.putInt(PreferencesConfig.HIGHEST_SCORE, finalScore);
editor.putInt("CATEGORY_"+ categoryNumber +"_SCORE", categoryScore);
editor.apply();
backToMainScreen = findViewById(R.id.back_to_screen);
backToMainScreen.setVisibility(View.INVISIBLE);
congrats = MediaPlayer.create(this, R.raw.mario_song);
congrats.start();
fadeInButton = AnimationUtils.loadAnimation(this, R.anim.fadein);
blinkingButton = AnimationUtils.loadAnimation(this,R.anim.blinking_back_button_anim);
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
backToMainScreen.startAnimation(fadeInButton);
backToMainScreen.startAnimation(blinkingButton);
backToMainScreen.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
congrats.release();
Intent intent = new Intent(CongratsScreen.this, CategoriesActivity.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
finishAffinity();
}
});
}
},1000);
}
@Override
public void onBackPressed() {
if (backPressedTime + 2000 > System.currentTimeMillis()) {
congrats.stop();
Intent intentBackToCategory = new Intent(CongratsScreen.this, CategoriesActivity.class);
startActivity(intentBackToCategory);
finishAffinity();
}
else {
Toast.makeText(this, "Press again to exit", Toast.LENGTH_LONG).show();
}
backPressedTime = System.currentTimeMillis();
}
}