Back to project page CoinFlipAndroidApp.
The source code is released under:
GNU General Public License
If you think the Android project CoinFlipAndroidApp listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package nathanyo.coinflip.coinflip_app; //from w w w .j a v a 2s. c om import android.app.Activity; import android.content.SharedPreferences; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.TextView; import java.util.Calendar; import java.util.Random; public class CoinFlipActivity extends Activity { public static int numHeads = 0; public static int numTails = 0; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_coin_flip); TextView numHeadsTextView = (TextView) findViewById(R.id.num_heads); TextView numTailsTextView = (TextView) findViewById(R.id.num_tails); SharedPreferences preferences = getPreferences(MODE_PRIVATE); numHeads = preferences.getInt("heads", 0); numTails = preferences.getInt("tails", 0); numHeadsTextView.setText(String.valueOf(numHeads)); numTailsTextView.setText(String.valueOf(numTails)); Button flipButton = (Button) findViewById(R.id.flip_button); flipButton.setOnClickListener(flipButtonListener); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.coin_flip, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. TextView numHeadsTextView = (TextView) findViewById(R.id.num_heads); TextView numTailsTextView = (TextView) findViewById(R.id.num_tails); int id = item.getItemId(); if (id == R.id.action_settings) { numHeads = 0; numTails = 0; numHeadsTextView.setText(String.valueOf(numHeads)); numTailsTextView.setText(String.valueOf(numTails)); return true; } return super.onOptionsItemSelected(item); } @Override public void onPause(){ super.onPause(); SharedPreferences.Editor editor = getPreferences(MODE_PRIVATE).edit(); editor.putInt("heads", numHeads); editor.putInt("tails", numTails); editor.commit(); } private OnClickListener flipButtonListener = new OnClickListener() { @Override public void onClick(View view) { flipCoin(); } }; private void flipCoin(){ Random coinFlipRandom = new Random(); int coinFlipResult = coinFlipRandom.nextInt(2); Calendar currentDate = Calendar.getInstance(); TextView resultTextView = (TextView) findViewById(R.id.coin_flip_result); TextView numHeadsTextView = (TextView) findViewById(R.id.num_heads); TextView numTailsTextView = (TextView) findViewById(R.id.num_tails); if (currentDate.DAY_OF_MONTH == 1 && currentDate.MONTH == currentDate.APRIL){ resultTextView.setText(R.string.result_on_its_side); }else if(coinFlipResult == 0){ resultTextView.setText(R.string.result_heads); numHeads++; numHeadsTextView.setText(String.valueOf(numHeads)); }else { resultTextView.setText(R.string.result_tails); numTails++; numTailsTextView.setText(String.valueOf(numTails)); } } }