If you develop Android apps that have a game character, you often need more functions than just the game itself. One example would be to enter a high score. If you can collect points in the app, it makes sense to save a high score and measure yourself against it. The app must then determine whether the previous high score has been broken or not. This article is about how to save and retrieve simple high scores with SharedPreferences in Android.

High scores with SharedPreferences in Android

It becomes more difficult if you want to save many scores locally (for example, a top 10 list with the ten best results) or create a global leaderboard. There are different solutions for both variants. We will concentrate here on the simple option of saving a high score locally. SharedPreferences in Android is well suited for this.

I myself use SharedPreferences in simple apps for which I only need to save a high score locally. One example is the NBA Player Quiz. For other apps, I use a global leaderboard.

How to save a high score with SharedPreferences in Android?

There are basically only three things I need to consider:

  1. I need an option to save a value permanently
  2. I need to be able to compare the value with other values
  3. If another value is higher than the currently saved value, this is the new high score

Sounds easy – and it is. As we will see below, the whole thing can be implemented with just a few lines of code.

Permanently in point 1 above means that the value should not be lost after the app or activity is closed. That would be pretty pointless for games such as a quiz. The high score would then only apply to the current session and the next time the app is started, the high score would return to 0. However, with SharedPreferences, information can be saved permanently.

How does the implementation with Java work?

Implementation with SharedPreferences is very simple in Android. We will concentrate here on the Java programming language (Kotlin is just as easy or even easier).

The declaration looks as follows:

SharedPreferences prefs = this.getSharedPreferences("MyHighscore", Context.MODE_PRIVATE);

The text between the quotation marks (here: MyHighscore) is freely selectable. Context.MODE_PRIVATE means that only the app in which the code is located has access to the stored value. prefs is the variable name and can therefore also be any name you want.

We can now store various values in the variable prefs. The data type int would be suitable for a highscore. To access this highscore, we would use the following code:

prefs.getInt("highscore", 0);

The highscore in quotation marks is again freely choosable. It is a key and is used to assign the correct value. If you want to read or change the highscore, you must specify this string. A key and a value are always linked in SharedPreferences. The default value is 0. The high score has the value 0 until another score greater than 0 is entered.

Let us now assume that we have a score (called score here) for the current round of the game. We want to compare this with the current high score. If the current score is higher, it is saved as the new high score. The code for this looks like this:

if(score > prefs.getInt("highscore", 0)) {
  SharedPreferences.Editor edit = prefs.edit();
  edit.putInt("highscore", score);
  edit.apply();
}

An application that compares the current score and a high score
And that’s basically it. As we can see, saving a high score with SharedPreferences in Android is very simple. Programming with Kotlin is at least as easy. Generally speaking, Kotlin generates less code than Java.

Information other than the high score can also be saved

SharedPreferences is not only suitable for saving the score itself, you can also save other data. In the context of a game, for example, it is conceivable that the user name could also be saved if one is available in the app. Saving the user name would look like this:

SharedPreferences.Editor edit = prefs.edit();
edit.putString("username", username);
edit.apply();

It would also be possible, for example, to save the date on which the high score was achieved.

Another good option is to save user preferences. Let’s assume, for example, that the user can select in an app whether hints are displayed or not. This can be saved as a simple number (0 or 1) or as a Boolean variable. SharedPreferences is very suitable for such functionalities.

The method with SharedPreferences is not suitable for saving strings in different languages. I have described how to do this in the article How to develop Android Apps in multiple languages.

Advantages of SharedPreferences

Saving high scores with SharedPreferences in Android Studio has the following advantages:

  • SharedPreferences is very easy to implement. It is therefore particularly suitable for Android beginners to take their first steps. This applies to development in both Java and Kotlin.
  • The high score is saved permanently and is not lost when the app or activity is closed.
  • You can store several values in one step and with little code, for example score, name and current date.
  • Not only is the implementation fast, but retrieving the data within the Android app is also very fast.

Disadvantages of SharedPreferences for saving high scores

On the other hand, there are the following disadvantages:

  • Scores are only saved locally. This means that if player A plays a game on his Android device and player B on his, both players cannot compare their scores. Both are trying to beat their own high score, so to speak. There are other options for global leaderboards that are somewhat more difficult to implement.
  • SharedPreferences is not suitable for storing large amounts of data. Although it is possible to store lists there, other methods such as databases (SQLite) make more sense for larger local leaderboards.
  • Only primitive data types such as int, long, float, string or boolean can be saved. This method is not suitable for saving more complex data types.
  • If the app is uninstalled, the data saved with SharedPreferences is also lost. If the user reinstalls the app later, he or she must start from scratch.

Conclusion

SharedPreferences is probably the easiest way to save and retrieve a high score in Android. A high score can be implemented in this way with just a few lines of code and in just a few minutes. This method is therefore still a good option for simple applications.

Basically, we can say that if you want to store small amounts of data with an Android application, SharedPreferences is a very good option. For larger amounts of data, SQLite makes more sense. The same applies if you want to create a global leaderboard. I personally use dreamlo for this.


0 Comments

Leave a Reply

Avatar placeholder

Your email address will not be published. Required fields are marked *