Skip to content Skip to sidebar Skip to footer

Ad Is Not Visible. Not Refreshing Ad

I have made a free android game and I wanted to add a banner to it. I followed this tutorial and I don't know why it won't work. When I run my game on the emulator in LogCat I see

Solution 1:

I've found out using adMob defining the layout explicitely in a file could be a huge headache generator, as sometimes it might turn impredictable with some parameter and layout combination. What always seems to work is adding the AdView dynamically to some LinearLayout.

This should work:

finalLinearLayoutfragLayout= (LinearLayout) inflater.inflate(R.layout.your_layout, container, false);
finalAdViewadView=newAdView(getActivity());
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);

// Here you'll append the new AdViewfinalLinearLayoutpubliView= (LinearLayout) fragLayout.findViewById(R.id.publi);
publiView.addView(adView);

final AdRequest.BuilderadReq=newAdRequest.Builder();
// You should include a line like this for testing purposes,// but only after you've tested whether your AdView works!// This will prevent your ad being loaded each time you test// your ad, so it will prevent you being blocked from AdMob.// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");

finalAdRequestadRequest= adReq.build();
adView.loadAd(adRequest);

---- EDIT ----

This implementation is for Fragments, you probably are running an Activity and that's why you're getting those errors.

  • Replace getActivity() with this

  • The inflater simply (as it name says) inflates a new LinearLayout from a layout file. This means I have defined a layout which I've called your_layout and defined inside a LinearLayout. The inflater just creates an instance of that `LinearLayout. In your case probably it's not necessary.

I'm adding a code that probably will work for you:

finalLinearLayoutyourLayout= (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);

finalAdViewadView=newAdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);

yourLayout.addView(adView);

final AdRequest.BuilderadReq=newAdRequest.Builder();
// You should include a line like this for testing purposes,// but only after you've tested whether your AdView works!// This will prevent your ad being loaded each time you test// your ad, so it will prevent you being blocked from AdMob.// You'll find your device_id in the LogCat.
adReq.addTestDevice("your_device_id");

finalAdRequestadRequest= adReq.build();
adView.loadAd(adRequest);

Solution 2:

To show ads in libgdx you need to initialize your app for view. So, replace all this:

setContentView(R.layout.main);

// Look up the AdView as a resource and load a request.
adView = (AdView)this.findViewById(R.id.adView);
AdRequestadRequest=newAdRequest.Builder().build();
adView.loadAd(adRequest);

initialize (newRedSquare(), cfg);

With this:

RelativeLayoutlayout=newRelativeLayout(this);

requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
ViewgameView= initializeForView(newRedSquare(), cfg);

AdViewAdView=newAdView(this);
AdView.setAdSize(AdSize.SMART_BANNER);
AdView.setAdUnitId("***"); //The AdUnitId
AdRequest.BuilderadRequest=newAdRequest.Builder();
adRequest.addTestDevice("***"); //Your Test device if any
AdView.loadAd(adRequest.build());

layout.addView(gameView);

RelativeLayout.LayoutParamsadParams=newRelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
adParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
adParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);

layout.addView(AdView, adParams);

setContentView(layout);

Solution 3:

There is nothing especially wrong with your layout. Though I would probably change the layout_width for AdView to be:

android:layout_width="match_parent"

But it is not clear what

initialize (newRedSquare(), cfg);

is doing? I suspect it is altering the layout.

Post a Comment for "Ad Is Not Visible. Not Refreshing Ad"