Admob ads exist but are invisible


#1

So:

LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(320,480);

    adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId(AD_UNIT_ID);
                         
    AdRequest adRequest = new AdRequest.Builder()
    .build();
  
    adView.loadAd(adRequest);     

    // Adding full screen container
    addContentView(adView, adParams);  

I hardcoded the emulator’s resolution just for ease.
If I click at a certain position the browser opens and takes me to a website, so the ads work. The problem is I can’t make them appear.

What should I do?


#2

Do the following and it should ateast start showing… but still there seems to be a problem when you lock the screen … the ad will dissapear… still looking for a fix for that

    adView.setBackgroundColor(Color.BLACK);
    addContentView(adView, initialFrm);
    adView.setBackgroundColor(0); 

#4

Try this, this is using the new Google gms ads.

In your onCreate:-

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
			// RelativeLayout LinearLayout;
			try {

				float px = getContext().getResources().getDisplayMetrics().widthPixels;
				float width = getContext().getResources().getDisplayMetrics().xdpi;
				float py = getContext().getResources().getDisplayMetrics().heightPixels;
				float height = getContext().getResources().getDisplayMetrics().ydpi;
				float scaley = py / height;
				float scale = px / width;
				scale /= 4;
				scaley /= 2;
				float fTopOffset = 50 * scaley;

				adParams = new LinearLayout.LayoutParams((int) px, -100); // fTopOffset);

				// Create an ad.
			    adView = new AdView(this);
			    adView.setAdSize(AdSize.BANNER);
			    adView.setAdUnitId("your ad id");  //REPLACE!!!

			    // Create an ad request. Check logcat output for the hashed device ID to
			    // get test ads on a physical device.
			    AdRequest adRequest = new AdRequest.Builder()
			        .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
			        .addTestDevice("your test device number"); // REPLACE !!!
			        .build();

			    // Start loading the ad in the background.
			    adView.loadAd(adRequest);
			    
				addContentView(adView, adParams);
				adView.setVisibility(View.INVISIBLE);

			} catch (Exception e) {

			}

Note the “adView.setVisibility(View.INVISIBLE);” makes this init invisible, just comment out that line if you want to see the ads straight away.

You can hide and show the ads with functions:-

public void hideAds() {
			if (adView.getVisibility() == AdView.VISIBLE) {
				adView.post(new Runnable() {
					public void run() {
						adView.setVisibility(AdView.INVISIBLE);
					}
				});
			}
}
public void showAds() {
			// AdMob
			if (adView.getVisibility() == AdView.INVISIBLE) {
				adView.post(new Runnable() {
					public void run() {
						adView.setVisibility(AdView.VISIBLE);
					}
				});
			}
}

#5

Also in my case the ad dissapears on clickin the lock screen… and comes back on when revmob loads… how do i fix it?


#6

ok, the first answer worked fine.