React-native-admob: Set size

Created on 3 Dec 2017  路  6Comments  路  Source: sbugert/react-native-admob

Is it possible to manually set the size of Ad? For example, I need to get a banner with 280x80. When I use SmartBanner, it not work because my banner is too big I think. Has it anyway to change the default size banner? Thanks

enhancement

Most helpful comment

@potench hey.. so this should help:
https://github.com/nfl/react-native-admob/blob/fd483b8f0de0c0fdc8556da446aed7346178529d/ios/RCTConvert%2BGADAdSize.m#L27-L28

This extra else should support any custom size now. Instead of passing "banner" you need to pass the size on this format: "{width,height}", e.g.: "{220,30}"

All 6 comments

Currently you only use one of the predefined constants, as can be seen here: https://github.com/sbugert/react-native-admob/blob/a6d1909f54fdf53d0912bc56c21ca77ae40a70cf/ios/RCTConvert%2BGADAdSize.m#L5-L29

I'll label this as an enhancement, because I can see how a value like "280x80" should be possible as well.

Looking to help PR this, any advice on where to start? Here's my best guess so far:
https://github.com/nfl/react-native-admob/blob/master/ios/RNDFPBannerView.m#L65-L77

Currently this only allows an enumerable adSize.

- (void)setValidAdSizes:(NSArray *)adSizes
{
    NSMutableArray *validAdSizes = [[NSMutableArray alloc] initWithCapacity:adSizes.count];
    [adSizes enumerateObjectsUsingBlock:^(id jsonValue, NSUInteger idx, __unused BOOL *stop) {
        GADAdSize adSize = [RCTConvert GADAdSize:jsonValue];
        if (GADAdSizeEqualToSize(adSize, kGADAdSizeInvalid)) {
            RCTLogWarn(@"Invalid adSize %@", jsonValue);
        } else {
            [validAdSizes addObject:NSValueFromGADAdSize(adSize)];
        }
    }];
    _bannerView.validAdSizes = validAdSizes;
}

Can we extend this to allow custom ad sizes? (Here's an example: https://github.com/googleads/googleads-mobile-ios-examples/blob/master/Swift/advanced/APIDemo/APIDemo/DFPMultipleAdSizesViewController.swift#L56-L82)

Maybe an else if when a [[width],[height]] is provided?

let customGADAdSize = GADAdSizeFromCGSize(CGSize(width: 120, height: 20))
validAdSizes.append(NSValueFromGADAdSize(customGADAdSize))

@potench hey.. so this should help:
https://github.com/nfl/react-native-admob/blob/fd483b8f0de0c0fdc8556da446aed7346178529d/ios/RCTConvert%2BGADAdSize.m#L27-L28

This extra else should support any custom size now. Instead of passing "banner" you need to pass the size on this format: "{width,height}", e.g.: "{220,30}"

Admob provide sizes->
https://support.google.com/adsense/answer/6002621?hl=en#top

In file RNAdMobBannerViewManager.java
private AdSize getAdSizeFromString(String adSize) {

     AdSize square= new AdSize(125,125);

switch (adSize) {
case "square":
return square;
}

use=====
adSize="square"
adUnitID="ca-app-pub-3940256099942544/6300978111"
onAdFailedToLoad={error => console.error(error)}
/>

@fabio-cerdeiral-ck updating your answer...Now we can give admob banner size this '320x50' ( 320 is width and 50 is height) for custom banner size instead of BannerAdSize.LARGE_BANNER etc.
example:

<BannerAd unitId={TestIds.BANNER} size= '320x50' requestOptions={{ requestNonPersonalizedAdsOnly: true, }} onAdLoaded={() => { console.log('Advert loaded'); }} onAdFailedToLoad={(error) => { console.error('Advert failed to load: ', error); }} />

Was this page helpful?
0 / 5 - 0 ratings