9

I'm unlocking achievement using this simple method from developers docs:

Games.Achievements.unlock(getApiClient(), "my_achievement_id");

Achievement unlocks, but no popup shows up. There is also no popup when logged in to Google Play Games - which is connected.

Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157

5 Answers5

14

Just add a view to the layouts you want to display achievements on like this:

<FrameLayout
        android:id="@+id/gps_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

When you have your layout ready you neet to execute this inside your Activity or Fragment:

Games.setViewForPopups(getApiClient(), findViewById(R.id.gps_popup));

You have to be sure, that your GoogleApiClient is connected though, otherwise your app will crash.

DeadChex
  • 4,379
  • 1
  • 27
  • 34
Jacek Kwiecień
  • 12,397
  • 20
  • 85
  • 157
  • 2
    Does the GoogleApiClient actually have to be connected? What if it's properly instantiated but not actually connected? I don't see why being connected should have any impact on setting a view. – Tosen Apr 21 '15 at 13:05
  • 2
    The link to your blog article appears to be broken. – Andrew Morton Jul 27 '18 at 12:39
8
<FrameLayout
        android:id="@+id/gps_popup"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp" />

This is the same in Jacek Kwiecień answer

GamesClient gamesClient = Games.getGamesClient(MainActivity.this, GoogleSignIn.getLastSignedInAccount(context));
gamesClient.setViewForPopups(findViewById(R.id.gps_popup));

This changed because setViewForPopups with 2 parameters is deprecated.

user3782779
  • 1,669
  • 3
  • 22
  • 36
5

Jacek and user3782779 answers didn't work for me, I had to do the following:

GamesClient gamesClient = Games.getGamesClient(this, GoogleSignIn.getLastSignedInAccount(this));
gamesClient.setViewForPopups(findViewById(android.R.id.content));
gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);
thiagolr
  • 6,909
  • 6
  • 44
  • 64
0

Had the same problem. I have solved it by adding icon to the achievement. I am not kidding, it is really strange but it started to work after that. Please note that I am talking about not published project, I was just testing my app and wondering what is going on.

Nezbeda
  • 131
  • 2
  • 12
0

The ONLY view that worked for my case of having multiple activities was:

activity.window.decorView.findViewById(android.R.id.content)

I had the same problem with the achievement popup. The "Welcome Back" popup was showing correctly by just using my own View, but once I started opening other screens where I wanted to show the achievement unlocked popup, it was not working. The only thing that ended up working was using the content view from the decorView

val gamesClient = Games.getGamesClient(activity, googleSignInAccount)
gamesClient.setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
gamesClient.setViewForPopups(activity.window.decorView.findViewById(android.R.id.content))

You can call this code from any new activity you open and the pop-up will show up there as soon as you unlock your achievements.

Benny
  • 1,650
  • 17
  • 20