1

Hello here is the code i am using to display two different adunit ids from two different admob account . But banners ads are not seen in app.

enter code here
private void setUpAds(){
    MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");         

    AdView bannerAdView = new AdView(this);
    int idSelection = random.nextInt(2);
    // Uniform distribution
    if (idSelection == 0) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    bannerAdView.setAdUnitId(adId);

    bannerAdView.setAdSize(AdSize.BANNER);
    AdRequest adRequest6 = new AdRequest.Builder().addTestDevice("E4D1201527AD69E0FD7A0551277A5232").build();
    bannerAdView.loadAd(adRequest6);

}



@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);
  • I don't know if it's permitted, you should be very careful with their policies. They have a soft trigger finger for suspending ads or banning people. If it' permitted, how do you wish to display ads? Once from your id, then from his or randomly pick one and display from it? I can write a detailed answer depending on behavior you wish to implement – Thracian Mar 23 '18 at 16:53
  • I want to randomly pick app id between mine and mine friends app id ? – Nikhil Komalan Mar 24 '18 at 18:49

1 Answers1

0

Create both ad units, and use a Random to pick a banner id. You can also use weighted selection which one id is more likely to be selected.

private static final String BANNER_ID_1 = "ca....";
private static final String BANER_ID_2 = "ca....";
private Random random = new Random();
private String adId = BANNER_ID_1;

private AdView bannerAdView;


private void setUpAds() {
    bannerAdView = new AdView(context);
    int idSelection = random.nextInt(2);
    // Uniform distribution
    if (idSelection == 0) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }
    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8) {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }

    bannerAdView.setAdUnitId(adId);

    // Or you can use weighted distribution
    idSelection = random.nextInt(10);
    // %80 chance first, %20 second
    if (idSelection < 8)

    {
        adId = BANNER_ID_1;
    } else {
        adId = BANER_ID_2;
    }

    bannerAdView.setAdUnitId(adId);
}
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • Okay i will look to implement this. But don't we need to add App id of my friend below my App id declaration ? – Nikhil Komalan Mar 25 '18 at 08:13
  • BANNER_ID_1, and BANNER_ID_2 are your and your friend's banner ids you retrieved from Admob. – Thracian Mar 25 '18 at 08:30
  • Yes but what about app id ? When we generate ads we get one app id and adunit id. """MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713"); "" Do i need to initalize the adunit id for both me and my friends account ? – Nikhil Komalan Mar 25 '18 at 08:45
  • No, you don't. You display ads using ad ids, not app ids. Please, check admob code to set up banner ads. You create banner and interstitial ads in AdMob page, then get ad ids for corresponding ad and use them when setting ads with `setAdUnitId(String adUnitId)` method – Thracian Mar 25 '18 at 09:07
  • I am unable to see the banner ads in my app, i have added the code above in my question. Kindly modify it – Nikhil Komalan Mar 25 '18 at 10:43
  • Check out a tutorial for setting up banner ads or interstitial ads. You may be getting exception onAdLoadFailed() method with code 2 or 3. You should check out a tutorial and successfully implement ads, then modify it as i posted as answer. – Thracian Mar 25 '18 at 11:45
  • I know how to implement ads by using adview adunit in xml code and showing it up in Java. I have confirmed from admob, that we can use two different account adunit id. – Nikhil Komalan Mar 26 '18 at 16:21
  • So what if I show my friend ads on first activity, and show up my ads on second activity using normal admob implementation.Will both my and mine friend ads show up ? Or is it necessary to use random method? – Nikhil Komalan Mar 26 '18 at 16:23
  • @NikhilKomalan Did it work for you? I have two different admob accounts and i also want to do the same. so does this idea work? – Robert Williams Apr 02 '19 at 14:41