13

My current Android game employs BaseGameActivity.

My game employs Achievements, which are getting unlocked when required.

However, I do not ALWAYS see the PopUps related to the unlock event.

I know the popup only appears when you first unlock the Achievement.

Some popups appear fine, others (from different screens within my game) never appear.

What do I have to do to Guarantee the popups appear?

I have a feeling its related to this WARNING:

W/PopupManager(10725): You have not specified a View to use as content view for popups.

Falling back to the Activity content view which may not work properly in future versions
of the API. 
Use setViewForPopups() to set your content view.

I have called setViewForPopups() from within the activity my popups do not display in, however, I have never seen them.

How do you call setViewForPopups() so that your entire application never sees the WARNING messages shown above?

Mizan
  • 3
  • 3
Hector
  • 2,024
  • 15
  • 69
  • 142

6 Answers6

8

I have found a solution, by using the following code

Games.setViewForPopups(getApiClient(), getWindow().getDecorView().findViewById(android.R.id.content));

I can get popups to show. I now have a related issue. The popup doesn't display for very long.

I think this is due to the fact I have a custom animation into this activity.

Is there any way to increase how long a popup is visible?

ppreetikaa
  • 1,025
  • 2
  • 14
  • 22
Hector
  • 2,024
  • 15
  • 69
  • 142
  • Where are you using this code? I have the same warning but the only place I find this method used in my project is at the Games class by the GPS library. Should I use this code manually somewhere in the BaseGameActivity? Thank you. – luben Sep 10 '14 at 17:36
5

This worked for me.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.d(TAG, "onCreate");

    setContentView(R.layout.activity_main);

    // Create the Google API Client with access to Plus, Games and Drive
    // Also set the view for popups
    mGoogleApiClient = new GoogleApiClient.Builder(getApplicationContext())
            .addConnectionCallbacks(this)
            .addOnConnectionFailedListener(this)
            .addApi(Plus.API).addScope(Plus.SCOPE_PLUS_LOGIN)
            .addApi(Games.API).addScope(Games.SCOPE_GAMES)
            .addApi(Drive.API).addScope(Drive.SCOPE_APPFOLDER)
            .setViewForPopups(findViewById(android.R.id.content))
            .build();

}

android.R.id.content gives you the root element of a view, without having to know its actual name/type/ID. Check out Get root view from current activity

Community
  • 1
  • 1
Philip Belgrave-Herbert
  • 4,340
  • 2
  • 30
  • 38
5

Games.setViewForPopups is deprecated

To show popup, add the next code into your activity or fragment layout:

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

And add the next code after the code where you init your object of AchievementsClient class

 GamesClient gamesClient = Games.getGamesClient(MainActivity.this, googleSignInAccount);
 gamesClient.setViewForPopups(findViewById(R.id.container_pop_up));

where googleSignInAccount is GoogleSignInAccount's object

Kiryl Belasheuski
  • 2,483
  • 2
  • 23
  • 38
5

There has been some changes in recent update to play services framework. Use this instead so that you can see greeting popup and unlock popups as well.

GamesClient gamesClient = Games.getGamesClient(this, GoogleSignIn.getLastSignedInAccount(this));
gamesClient.setViewForPopups(findViewById(android.R.id.content));
gamesClient.setGravityForPopups(Gravity.TOP | Gravity.CENTER_HORIZONTAL);

Don't forget to import as required like-

import android.view.Gravity;
import com.google.android.gms.games.GamesClient;
Sajal Dutta
  • 7,258
  • 25
  • 33
0

For those of you struggling in Kotlin this is what works for me:

private fun signInSilently() {
    mGoogleSignInClient.silentSignIn().addOnCompleteListener(this)
    { task ->
        if (task.isSuccessful) {
            Log.d(LOG_TAG, "signInSilently(): success")
            mAchievementsClient = Games.getAchievementsClient(this, task.result!!)
            val gamesClient = Games.getGamesClient(this@AchievementsScreen, GoogleSignIn.getLastSignedInAccount(this)!!)
            gamesClient.setViewForPopups(findViewById(android.R.id.content))
            gamesClient.setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
        } else {
            Log.d(LOG_TAG, "signInSilently(): failure", task.exception)
            startSignInIntent()
        }
    }
}
Nick
  • 1,093
  • 1
  • 11
  • 21
0

This worked perfectly (Kotlin):

    Games.getGamesClient(this, googleSignInAccount)
        .setGravityForPopups(Gravity.TOP or Gravity.CENTER_HORIZONTAL)
    val gamesClient = Games.getGamesClient(this@MainActivity, googleSignInAccount)
    gamesClient.setViewForPopups(window.decorView.findViewById(android.R.id.content))
Bolling
  • 3,297
  • 22
  • 24