Juraj Pančík Juraj Pančík - 3 years ago 82
Android Question

AdMob won't show the banner until refresh or sign in to google plus

I've got a problem. My AdMob has been set up for some time now without any problem, but I noticed something wrong. Ad gets successfully loaded (i see message from ddms), but it won't show. It will get shown after periodical 60 seconds refresh or when I open up login to google plus. The problem happens only with Google Play Services AdMob and not with AdMobSDK jar. I'd switch to AdMob jar, however I'm using Google Play Game Services for leaderboards and achievements.

I suspect the problem is with view not being shown, or inproper settings.

So again, ad will show itself after 60 seconds (along with ad refresh) of waiting, or when I fire up the log in screen for google play services.

I'm adding my code, also I should mention that I've switched to new AdMob website and I repeat, that problem is not happening if I use AdMob jar file (the ad is then shown in 2-3 seconds like normal). I've cut the google play game services code (they don't affect this issue because i've tried in my other app without them, and the problem is still there).

MainActivity code:

public class MainActivity extends AndroidApplication {

public static enum AdsStatus {
SHOW_ADS, HIDE_ADS;
}

protected RelativeLayout layout;

protected static AdView adMobView;

public static class InnerHandler extends Handler {
WeakReference<MainActivity> mActivity;

InnerHandler(MainActivityactivity) {
mActivity = new WeakReference<MainActivity>(activity);
}

@Override
public void handleMessage(final Message msg) {
if(msg.obj instanceof AdsStatus) {
switch((AdsStatus)msg.obj) {
case SHOW_ADS:
mActivity.get().showAds();
break;
case HIDE_ADS:
mActivity.get().hideAds();
break;
}
}
}
}
protected Handler handler = new InnerHandler(this);

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// Create the layout
layout = new RelativeLayout(this);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);

// Create the libgdx View
View gameView = initializeForView(new MainApplicationListener(), true);
layout.addView(gameView);

//Ad Mob
final DisplayMetrics displayMetrics = MainActivity.this
.getApplicationContext().getResources()
.getDisplayMetrics();

if (displayMetrics.widthPixels >= 800 && displayMetrics.heightPixels >= 480) {
if(adMobView != null) {
adMobView.destroy();
}

adMobView = new AdView(AirDance.this);
adMobView.setAdUnitId(<MY_ID_IS_HERE>);
adMobView.setAdSize(AdSize.SMART_BANNER);

RelativeLayout.LayoutParams adParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
adParams.addRule(RelativeLayout.CENTER_HORIZONTAL);

AdRequest adRequest = new AdRequest.Builder()
.addTestDevice(<MY_DEVICE_IS_HERE>)
.build();
adMobView.loadAd(adRequest);

layout.addView(adMobView, adParams);
}

// Hook it all up
setContentView(layout);
}

@Override
protected void onResume() {
super.onResume();

AppRater.applicationLaunched(this, analytics);

if(adMobView != null) {
adMobView.resume();
}
}

@Override
protected void onPause() {
super.onPause();

if(adMobView != null) {
adMobView.pause();
}
}

@Override
protected void onDestroy() {
super.onDestroy();

if(adMobView != null) {
adMobView.destroy();
adMobView = null;
}
}

public void showAds() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(adMobView != null) {
adMobView.setEnabled(true);
adMobView.setVisibility(View.VISIBLE);
}
}
});
}

public void hideAds() {
runOnUiThread(new Runnable() {
@Override
public void run() {
if(adMobView != null) {
adMobView.setEnabled(false);
adMobView.setVisibility(View.GONE);
}
}
});
}
}


And here is AndroidManifest:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="..."
android:installLocation="auto"
android:versionCode="16"
android:versionName="1.2.6" >

<uses-sdk
android:minSdkVersion="9"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-feature android:name="android.hardware.screen.landscape"/>
<uses-feature android:name="android.hardware.touchscreen.multitouch" />

<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="MainActivity">

<meta-data android:name="com.google.android.gms.games.APP_ID"
android:value="@string/app_id" />

<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />

<activity
android:name="...MainActivity"
android:label="MainActivity"
android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>

<!-- adMob -->
<activity android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
</application>
</manifest>


EDIT :
okay, so ad gets visible after I lock and unlock screen with app open, also you can click on invisible ad

EDIT :
Okay I think I solved it. I just manually reload whole layout onAdLoad event. Anyway, this is just functional solution, it does not explain why it happens with Google Play
Services AdMob.

adMobView.setAdListener(new AdListener() {

@Override
public void onAdLoaded() {
super.onAdLoaded();

MainActivity.this.runOnUiThread(new Runnable() {

@Override
public void run() {
layout.requestLayout();
}
});
}

});

Answer Source

As nobody has replied with explanation, I'm going to consider this one solved. I just manually reload whole layout onAdLoad event. Anyway, this is just functional solution, it does not explain why it happens with Google Play Services AdMob.

adMobView.setAdListener(new AdListener() {

    @Override
    public void onAdLoaded() {
        super.onAdLoaded();

        MainActivity.this.runOnUiThread(new Runnable() {

            @Override
            public void run() {
                layout.requestLayout();
            }
        });
    }

});

Also, as mentioned by user3263204, you can try this

adMobView.setBackgroundColor(Color.BLACK);

to solve your problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download