package com.hyperling.apps.games; import android.content.Intent; import android.content.SharedPreferences; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity { private Button btnGameBird, btnGameCow, btnGamePig, btnGameChicken, btnGameCat, btnGameSquirrel, btnOptions, btnHighScores, btnHelp; private String TAG, sharedPreferencesKey, birdScoreKey, chickenScoreKey; private SharedPreferences sharedPreferences; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); TAG = getString(R.string.TAG); // Keys sharedPreferencesKey = getString(R.string.sharedPreferencesKey); birdScoreKey = getString(R.string.birdScoreKey); chickenScoreKey = getString(R.string.chickenScoreKey); // Shared Preferences sharedPreferences = getSharedPreferences(sharedPreferencesKey, MODE_PRIVATE); // Initialize buttons btnGameBird = (Button) findViewById(R.id.btnBirdGame); btnGameCow = (Button) findViewById(R.id.btnGameCow); btnGamePig = (Button) findViewById(R.id.btnGamePig); btnGameChicken = (Button) findViewById(R.id.btnGameChicken); btnGameCat = (Button) findViewById(R.id.btnGameCat); btnGameSquirrel = (Button) findViewById(R.id.btnGameSquirrel); btnOptions = (Button) findViewById(R.id.btnOptions); btnHighScores = (Button) findViewById(R.id.btnHighScores); btnHelp = (Button) findViewById(R.id.btnHelp); // Set up game buttons btnGameBird.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); btnGameCow.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); btnGamePig.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); btnGameChicken.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); btnGameCat.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); btnGameSquirrel.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doGameButton((Button) v); } }); // Set up other buttons btnOptions.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doOptionsButton(); } }); btnHighScores.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { doHighScoresButton(); } }); } @Override protected void onResume() { // Append high scores to button text int score; String newText; score = sharedPreferences.getInt(birdScoreKey, 0); Log.d(TAG, "MainActivity.onResume(): birdScore=" + score); if (score > 0) { newText = getString(R.string.btnGameBird) + '\n' + getString(R.string.btnHighScoreSubHeader).concat(" ") + score; Log.d(TAG, "MainActivity.onResume(): newText=" + newText); btnGameBird.setText(newText); } else{ btnGameBird.setText(getString(R.string.btnGameBird)); } score = sharedPreferences.getInt(chickenScoreKey, 0); if (score > 0) { newText = getString(R.string.btnGameChicken) + '\n' + getString(R.string.btnHighScoreSubHeader).concat(" ") + score; Log.d(TAG, "MainActivity.onResume(): newText=" + newText); btnGameChicken.setText(newText); } else{ btnGameChicken.setText(getString(R.string.btnGameChicken)); } super.onResume(); } // Start the game private void doGameButton(Button b){ Log.i(TAG, "MainActivity.doCowGameButton()"); // Only get the game name, not the high score String gameType, buttonText; buttonText = b.getText().toString(); int newLine = buttonText.indexOf('\n'); if (newLine > 0) { gameType = buttonText.substring(0, buttonText.indexOf('\n')); } else{ gameType = buttonText; } // Start the GameActivity Intent game = new Intent(MainActivity.this, GameActivity.class); game.setAction(gameType); startActivity(game); } // Go to OptionsActivity private void doOptionsButton(){ Log.i(TAG, "MainActivity.doOptionsButton()"); Intent options = new Intent(MainActivity.this, OptionsActivity.class); startActivity(options); } private void doHighScoresButton(){ Intent highScores = new Intent(MainActivity.this, HighScoreActivity.class); startActivity(highScores); } }