HOW TO - Create your custom Achievements list and Leaderboards with Google Play Games in Android

Google Play Games is a good library to include gamification elements into your app, such as Achievements and Leaderboards. In this tutorial, I will show how to implement your custom visualization for Achievements and Leaderboards.

I have been using Google Play Games Services for Android in a project that I am currently working on, and I have encountered that the API lacks some important things that I would like to do.

For instance, in order to present an Achievement list or a Leaderboard, the API provides an Intent that shows that for you. It is really nice to have that fallback, but usually it will not go well with the look and feel of your app/game. Besides, you don’t want your app to look like everyone else’s!

According to the documentation, it is possible to create your own visualization of Achievements and Leaderboards, but there is no direction on how to proceed with that (or at least, I was not able to find it).

After digging a little bit into the documentation, I found there isn’t a convenient method call that returns a list of all achievements or parts of a leaderboard. Thus, I have created a couple of gists to show you how to do it.

To be able to retrieve the achievements list, you have to proceed as follows:

Note that this is an asynchronous call. Now that you have a list of Achievements, you can display them in whatever way you prefer. Take a look at the documentation for the Achievement class to know what information is available.

To be able to retrieve the leaderboards, you have to provide a little bit of additional info, such as the leaderboard ID you want to retrieve, the time span you are looking for (daily, weekly or all time), and the collection (public or social). Check the documentation for LeaderboardVariant to see the possible values that these parameters can take.

With this gist, we are able to load either top scores or user-centered scores. Note that this piece of code is only retrieving at most 25 scores for the leaderboard you are requesting. Loading more result pages is left as an exercise for the reader. Once you have your score list, you can display it however you’d like. Take a look at the documentation for LeaderboardScore to know what information is available for you to display.

I hope you find this useful and if you have any comments or questions, please, leave them below!

DEVELOPMENT
google play games tutorial achievements leaderboards

Dialogue & Discussion