1

I'm new to InMobi and I'm trying to implement a banner ad to my app. Here's my code

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setUpBannerAd();
}
private void setUpBannerAd(){
    InMobiSdk.init(MainActivity.this, MY_ID);
    InMobiBanner bannerAd = findViewById(R.id.banner);
    bannerAd = new InMobiBanner(MainActivity.this, BANNER_ID);
    bannerAd.setListener(new InMobiBanner.BannerAdListener() {
        @Override
        public void onAdLoadSucceeded(InMobiBanner inMobiBanner) {
            Toast.makeText(MainActivity.this, "sucess", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onAdLoadFailed(InMobiBanner inMobiBanner,
                                   InMobiAdRequestStatus inMobiAdRequestStatus) {
            Toast.makeText(MainActivity.this, "fail", Toast.LENGTH_SHORT).show();

        }

        @Override
        public void onAdDisplayed(InMobiBanner inMobiBanner) {
        }

        @Override
        public void onAdDismissed(InMobiBanner inMobiBanner) {
        }

        @Override
        public void onAdInteraction(InMobiBanner inMobiBanner, Map<Object, Object> map) {
        }

        @Override
        public void onUserLeftApplication(InMobiBanner inMobiBanner) {
        }

        @Override
        public void onAdRewardActionCompleted(InMobiBanner inMobiBanner, Map<Object, Object> map) {
        }
    });
    bannerAd.load();

}

} I think i have all the needed imports as well as manifest declarations, but not entirely sure because I'm new to the platform. Test ads are turned on. Toasts do NOT even show up. Thanks for replies!

1 Answers1

0
  1. You need to add the banner to the view hierarchy once its created. Something like this:

    RelativeLayout adContainer = (RelativeLayout) findViewById(R.id.ad_container);
    RelativeLayout.LayoutParams bannerLp = new 
    RelativeLayout.LayoutParams(640, 100); 
    bannerLp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
    bannerLp.addRule(RelativeLayout.CENTER_HORIZONTAL);
    adContainer.addView(bannerAd, bannerLayoutParams);</code>
    
  2. It looks like you created the banner twice. Once in XML, and once in code:

    //the banner is created below by reference to xml
    InMobiBanner bannerAd = findViewById(R.id.banner);
    //the banner is created below in code
    bannerAd = new InMobiBanner(MainActivity.this, BANNER_ID);</code>
    

Check out this guide for more details: https://support.inmobi.com/monetize/android-guidelines/banner-ads-for-android/

Paul Roub
  • 36,322
  • 27
  • 84
  • 93
markerj
  • 1
  • 1