0

I'm currently learning android app development and on the very beginning phase. I built this game and now i want to set store and display high score as Textview on my screen. I'd really appreciate if anyone could help me out. thanks!

    package com.princeghimire.clickmeter;

    import android.content.DialogInterface;
    import android.media.MediaPlayer;
    import android.os.Bundle;
    import android.os.CountDownTimer;
    import android.support.v7.app.AlertDialog;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.TextView;


    public class MainActivity extends AppCompatActivity{

    TextView tv_time, tv_clicks;
    Button b_start, b_click;

    CountDownTimer timer;
    int time = 10;
    int clicks = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setLogo(R.drawable.iconnn);
        getSupportActionBar().setDisplayUseLogoEnabled(true);
        tv_time = (TextView) findViewById(R.id.tv_time);
        tv_clicks = (TextView) findViewById(R.id.tv_clicks);
        b_start = (Button) findViewById(R.id.b_start);
        b_click = (Button) findViewById(R.id.b_click);

        b_start.setEnabled(true);
        b_click.setEnabled(false);

        timer = new CountDownTimer(10000, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                time--;
                tv_time.setText("Time: " + time);
            }

            @Override
            public void onFinish() {
                b_start.setEnabled(true);
                b_click.setEnabled(false);
                tv_time.setText("Time: 10" );
                AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                builder.setTitle("Time's Up Buddy!");
                builder.setMessage("Your Score Is: " + clicks);
                builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int which) {
                        dialogInterface.dismiss();
                        // finish();
                    }
                });
                AlertDialog alertDialog = builder.create();
                alertDialog.show();
            }
        };

        b_click.setOnClickListener(new View.OnClickListener(){
            public void onClick (View v){
                clicks++;
                //  switchh.start();
                tv_clicks.setText("Your Clicks: " + clicks);
            }
        });

        b_start.setOnClickListener(new View.OnClickListener(){
            public void onClick (View v){
                timer.start();
                b_start.setEnabled(false);
                b_click.setEnabled(true);
                clicks = 0;
                time = 10;
                tv_time.setText("Time: " + time);
                tv_clicks.setText("Your Clicks: " + clicks);
            }
        });


    }
    public void onBackPressed()
    {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("Are you sure you want to EXIT?")
                .setCancelable(false)
                .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        finish();
                    }
                })
                .setNegativeButton("No", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int id) {
                        dialog.cancel();
                    }
                });
        AlertDialog alert = builder.create();
        alert.show();
    }
}
mehrdad khosravi
  • 2,228
  • 9
  • 29
  • 34

2 Answers2

0

Basically this is the procedure:

  1. Create instance of SharedPreferences and SharedPreferences.Edit SharedPreferences sharedPref = .... AND SharedPreferences.Edit editor = .....
  2. Call this in where your game ends if (sharedPref.getInt("highscore",0) < clicks) { editor.putInt("highscore",clicks).apply() }

The above code will first check the last score you saved in "highscore" key and if your current clicks is higher it will replace it with it, and if this is the first game so the default value will be taken as 0 so the first game's score will be saved as highscore unless its 0 again.

I've made almost the same game, its Taplay and on PlayStore btw.

Good Luck.

Recomer
  • 178
  • 2
  • 12
0

You can use SharedPreferences, I suggest you to use Sqlite if you want to keep track of the user game statistics.