-1

I developed a clicker program that when I click on a button then strata counting clicks and after 15 seconds button goose disabled and I want **how much I clicked in 15 seconds that store as a high score and when I cross that high score then store my new high score show on same activity **

  • 1
    ....and the question is? – Opiatefuchs Nov 17 '16 at 18:39
  • ^^ Can you elaborate on what you are wanting and what you have tried? – Scott P Nov 17 '16 at 18:46
  • What do you mean? – user3476090 Nov 17 '16 at 18:46
  • I have no idea where to start i want just code for that you can see example – user3476090 Nov 17 '16 at 18:48
  • sorry, but that´s a really unclear question. What´s with that code you posted? It seems that it is what you want. Look, in that forum you can post something if you got problems, errors, if you stuck on a tricky part of code. But such questions like "give me the code" are absolutely off topic. Please explain more detailed. Does it throw an exception? Anything that does not work? Some other problem? – Opiatefuchs Nov 17 '16 at 18:56

2 Answers2

1

you can save and check your high score by using sharedpreferences. Like this:

these lines Under set_ContentView at the start:

String PREFS_GAME ="your package name";
SharedPreferences sp = getSharedPreferences(PREFS_GAME,Context.MODE_PRIVATE);
final Integer oldrec  = sp.getInt("record",0);

then write this code in setOnclicklistenre:

if (newrec>oldrec){
                sp.edit().putInt("record",new rec).commit();
                Toast.makeText(MainActivity.this,"your new record is :"+newrec, Toast.LENGTH_SHORT).show();
                }
0

i recomend:

public class MainActivity extends AppCompatActivity {

private int clicks = 0;
private TextView mTextView;
Button bt_restart;
int high;
public static final String MyPREFERENCES = "MyPrefs" ;
public static final String HIGHSCORE = "high" ;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    SharedPreferences prefs = getSharedPreferences(MyPREFERENCES, MODE_PRIVATE); 
    high = prefs.getInt(HIGHSCORE, 0); 
    bt_restart = (Button)findViewById(R.id.restart);
    bt_restart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent restartIntent = getBaseContext().getPackageManager()
                    .getLaunchIntentForPackage(getBaseContext().getPackageName());
            restartIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            startActivity(restartIntent);
        }
    });

    mTextView = (TextView) findViewById(R.id.total_textview);
    mTextView.setVisibility(View.VISIBLE);

    Button button = (Button) findViewById(R.id.count_button);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(final View v) {
            final Button b = (Button)v;
            if (clicks == 0){
                // Means its the first time that a user click the button
                // Start a thread that is going to disable the button after 5 seconds from first click
                new CountDownTimer(15000, 1000) {
                    public void onTick(long millisUntilFinished) {
                        b.setText(millisUntilFinished / 1000 + " Seconds");
                    }

                    public void onFinish() {
                        b.setText("Time up");
                        b.setEnabled(false);
                        // Showing user clicks after button is disabled
                        showClicks();
                    }
                }.start();
            }
            // Here we are just counting  . . . . including the first click
            countClicks();
        }
    });

}


private void countClicks(){
    ++clicks;
    mTextView.setText(Integer.toString(clicks));

    // You can update your text view here
}
private void showClicks(){
    mTextView.setText(String.valueOf(clicks)+"Clicks");
    mTextView.setVisibility(View.VISIBLE);
    if(clicks > high){
        SharedPreferences.Editor editor = getSharedPreferences(MY_PREFS_NAME, MODE_PRIVATE).edit();
        editor.putInt(HIGHSCORE, clicks);
        editor.commit();
        high = prefs.getInt(HIGHSCORE, 0); 
    }    
}

then if you you want to show the highscore use a textview and in the setText use String.valueOf(high), i don't know if the textview would change when change the variable high maybe you must the textview.setText everyTima you edit the variable high (code based on that link: Android Shared preferences example)

Community
  • 1
  • 1
dpart
  • 86
  • 9
  • "+1" I apricate your responce its not working coming erro can you please do me a faver just tell on GitHub ho to do it where i can copy and past – user3476090 Nov 17 '16 at 19:24
  • the application crushes? – dpart Nov 17 '16 at 19:28
  • these are the errors : Error:(89, 6) error: reached end of file while parsing Error:Execution failed for task ':app:compileDebugJavaWithJavac'. > Compilation failed; see the compiler error output for details. – user3476090 Nov 17 '16 at 19:30
  • Line number is not showing but when i click on error it leade to me end of the code and that code is "}" – user3476090 Nov 17 '16 at 19:38
  • try now, i edited the answer and put a link of another response of using sharedpreferences – dpart Nov 17 '16 at 19:41
  • the code modification was mine, based on this link http://stackoverflow.com/questions/23024831/android-shared-preferences-example i edited the answer for you including the link – dpart Nov 17 '16 at 19:52