0

I am trying to initialize AdMob ad programatically, without xml code, but I am not getting shown anything, and I am receiving these warnings in LogCat enter image description here

And here is how I am initializing the ad in OnCreate:

AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("some id here");
    adView.loadAd(adRequest);

What is wrong?

Borislav
  • 909
  • 3
  • 10
  • 25
  • 1
    You aren't attaching your AdView to the layout. See: http://stackoverflow.com/questions/6216547/android-dynamically-add-views-into-view – Morrison Chang Apr 28 '15 at 15:07
  • in regards to the above comment is that the only codes with respect to your adview? – Elltz Apr 28 '15 at 15:53
  • The problem is that I need to do this completely programatically and without layouts, because the code is for a GameMaker extension, and I don't know if providing a layout would work for it – Borislav Apr 29 '15 at 06:13

1 Answers1

1

You should post your complete code. If you haven't attached the adView to your layout, you can do so in the following manner:

RelativeLayout.LayoutParams adViewParams = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        adViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL  | RelativeLayout.ALIGN_PARENT_TOP);
        RelativeLayout layout = new RelativeLayout(this);
        layout.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
                LayoutParams.MATCH_PARENT));
     AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("some id here");
    adView.loadAd(adRequest);
    layout.addView(adView, adViewParams);

This solution assumes your layout is defined programatically. If your layout is defined via xml, you can use the Layout inflater to inflate it and attach the addView

Nana Ghartey
  • 7,901
  • 1
  • 24
  • 26
  • The problem is that I need to do this completely programatically and without layouts, because the code is for a GameMaker extension, and I don't know if providing a layout would work for it – Borislav Apr 29 '15 at 06:13
  • Without layouts? The adview must be attached to a layout eventually. If it's an extension you can implement it as an interface. You can also try interstitials. It'll be difficult helping without seeing the rest of your code – Nana Ghartey Apr 29 '15 at 23:07