Implementing native admob view in Android
Hi,
Here's some snippet of my code. However
- I could only see white space without the ad
- I tried debugging and it doesn't hit createViewInstance method
Any idea why? I tried implementing dummy prop too. I'm on RN 0.14.0-RC
ReactAdMobManager.java
public class ReactAdMobManager extends SimpleViewManager<AdView> {
private AdView mAdView;
@ReactProp(name = "src")
public void setSrc(AdView view, String src) {
Log.d("ReactAdMobManager", "inside set src");
}
public static final String REACT_CLASS = "RCTAdMobView";
@Override
public String getName() {
return REACT_CLASS;
}
@Override
public AdView createViewInstance(ThemedReactContext context) {
Log.d("ReactAdMobManager", "inside create view instance");
mAdView = new AdView(context);
mAdView.setAdSize(AdSize.FULL_BANNER);
mAdView.setAdUnitId("xxxxx");
mAdView.setAdListener(new AdListener() {
@Override
public void onAdLoaded() {
Log.d("ReactAdMobManager", "ad loaded");
}
});
// Create an ad request.
AdRequest adRequest = new AdRequest.Builder().addTestDevice("50AC32B853DA4B3E64C3B10BC26D0380").build();
//adRequest.
mAdView.loadAd(adRequest);
return mAdView;
}
}
ReactAdMobPackage.java
public class ReactAdMobPackage implements ReactPackage {
@Override
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
return Collections.emptyList();
}
@Override
public List<Class<? extends JavaScriptModule>> createJSModules() {
return Collections.emptyList();
}
@Override
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
return Arrays.<ViewManager>asList(new ReactAdMobManager());
}
}
MainActivity.java
mReactInstanceManager = ReactInstanceManager.builder()
.setApplication(getApplication())
.setBundleAssetName("index.android.bundle")
.setJSMainModuleName("index.android")
.addPackage(new MainReactPackage())
.addPackage(new ReactAdMobPackage()) // <--- add fab package here
.setUseDeveloperSupport(BuildConfig.DEBUG)
.setInitialLifecycleState(LifecycleState.RESUMED)
.build();
AndroidAdMobView.js
var { PropTypes, requireNativeComponent } = require('react-native');
var iface = {
name: 'AdMobView',
propTypes: {
src: PropTypes.string
}
}
module.exports = requireNativeComponent('RCTAdMobView', iface);
Usage
var AdView = require('./AndroidAdmobView');
//at render
<AdView style={{height:50}} text="foo" />
updated with my latest code.
my onAdLoaded listener got fired. however it's still not visible on my screen.
From the logs I saw
- W/cr.BindingManager﹕ Cannot call determinedVisibility() - never saw a connection for the pid: 24719
- I/Ads﹕ Ad is not visible. Not refreshing ad.
Wild guess: does AdView implement the onCreate and onResume methods? If yes try calling them directly in createViewInstance (or subclass AdView and call onCreate and onResume)
yes i just tried that too, still showing blank
Do you have any news on this? I am needing the same thing, so before I start from zero, I'd like to help you getting this.
I'm sorry @rodripf, I don't.
I tried asking in stackoverflow and there's no news too
can you let me know if you managed any findings? thanks
Hi steve! Could you share a repository with the code you used to start from that? Let's make it! Cheers
@rodripf the required codes are all up there!
Is there an update on this?
I reach the same point as you @steve-ng. In the catlog the ad request is successful but it doesn't shows. And admob knows that it is not showing because in the log it says "the ad is not being showed. re scheduling reload to 60 sec from now.". I've added some things to the scenario: added google play services to the compilation, added the required permissions and activity to the android manifest. I also tried updating the view once the ad is loaded, once I click a button, but with no success.
When I'm back home I'll upload my code to a repo so we can work with the same things.
I was not sure what to put into the repository and what not to make it usable, but I did my best. Here is it: https://github.com/rodripf/react-native-android-admob To note here, as commented before: what I added to manifest.xml and to the gradle files. But not success after all :-( Any other idea coming?

The example above mostly works, but does not on an initial load. The same example, when run on a device and there is an orientation change (android destroys and recreates views when that happens) the Ad loads. The loading of the ad needs to be removed from the render cycle to after, perhaps via a call in componentDidMount?
I think I solved it. The problem is that the AdView has zero width and zero height until loaded. I attached an AdListener and do some layout and measure magic when the ad is loaded.
I added it to my AdMob Library. It's far from perfect, but pull requests are welcome!
@sbugert I mostly followed your example and was able to get it working. However, I was able to simplify things.
SimpleViewManagerspecializes onAdView.createViewInstanceloads the Ad and returns an instance.- My
onAdLoadedcallback is as follows:
@Override
public void onAdLoaded() {
super.onAdLoaded();
Log.d(REACT_CLASS, "onAdloaded");
int width = AdSize.BANNER.getWidthInPixels(reactContext);
int height = AdSize.BANNER.getHeightInPixels(reactContext);
mAdview.measure(width, height);
mAdview.layout(0, 0, width, height);
}
On the Javascript side, I wrap the nativeView within a View which is styled to the size of the ad ie 320x50 for the banner. Wrapping it in a Viewis required so that the ad stays within the layout and does not jump to the top left of the parent view.
HTH - happy to send pull requests or put this code up if anyone is interested
@kritikinfo do you think u are able to provide a gist too? Thanks
@steve-ng Sure - I'll put up the whole thing into a gist and reference it here.
@kritikinfo Thanks for the idea to simplify things!
I found that mAdview.layout(0, 0, width, height); will break when using the banner in a flex layout.
Something like mAdview.layout(originalLeft, originalTop, width, originalTop + height); resolves this.
Also, your solution lacks the ability to change the banner size with a prop. This is why I added an event to style the javascript view with the appropriate size.
I got everything working now (still testing) with events and props and will push the changes later to my repo :-)
@steve-ng https://gist.github.com/kritikinfo/1bc92d2a07b3b1516a9d
The only thing special is in the onAdLoaded as per previous comments.
@sbugert eagerly waiting for the updates!
@kritikinfo I did not yet succeed in changing the banner's props on the fly because calling setAdSize() for changing is illegal. Creating a new instance didn't work either because the view passed to a prop method is final. The only solution I came up with is (again) wrapping a ReactViewGroup and creating a new AdView everytime a prop changes. Here is a gist with my current code.
Do you have any ideas how to improve it?
@steve-ng @kritikinfo Have any luck since this discussion?