Ad Is Not Visible. Not Refreshing Ad
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:
final LinearLayout fragLayout = (LinearLayout) inflater.inflate(R.layout.your_layout, container, false);
final AdView adView = new AdView(getActivity());
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
// Here you'll append the new AdView
final LinearLayout publiView = (LinearLayout) fragLayout.findViewById(R.id.publi);
publiView.addView(adView);
final AdRequest.Builder adReq = new AdRequest.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");
final AdRequest adRequest = 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()withthisThe
inflatersimply (as it name says) inflates a newLinearLayoutfrom a layout file. This means I have defined a layout which I've calledyour_layoutand defined inside aLinearLayout. 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:
final LinearLayout yourLayout = (LinearLayout) findViewById(R.id.your_linearlayout_id_where_you_want_to_put_your_adview);
final AdView adView = new AdView(this);
adView.setAdUnitId("your-admob-id");
adView.setAdSize(AdSize.BANNER);
yourLayout.addView(adView);
final AdRequest.Builder adReq = new AdRequest.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");
final AdRequest adRequest = 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);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
initialize (new RedSquare(), cfg);
With this:
RelativeLayout 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);
View gameView = initializeForView(new RedSquare(), cfg);
AdView AdView = new AdView(this);
AdView.setAdSize(AdSize.SMART_BANNER);
AdView.setAdUnitId("***"); //The AdUnitId
AdRequest.Builder adRequest = new AdRequest.Builder();
adRequest.addTestDevice("***"); //Your Test device if any
AdView.loadAd(adRequest.build());
layout.addView(gameView);
RelativeLayout.LayoutParams adParams =
new RelativeLayout.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 (new RedSquare(), cfg);
is doing? I suspect it is altering the layout.
Post a Comment for "Ad Is Not Visible. Not Refreshing Ad"