If you’re looking for a way to show ads in your Android app without including any ad network SDKs, read on. I just created a video tutorial showing how to get set up with LeadBolt ads in an Android WebView without using their SDK at all.
If you missed my previous review of LeadBolt, take a look now. I’ve seen an eCPM of $3.42 so far this month with standard banners, so I’m still pleased with the performance of LeadBolt.
Then watch this video to see how you can get set up in just a few minutes.
Don’t forget to click like if you found the video helpful! You can download the source code for this project, or look below for a quick summary.
Note that you’ll need to sign up for LeadBolt to see the ads yourself, as no ads will display without a valid ID. The signup process is pretty painless, though.
Code changes
Set up the webview in the layout
<WebView android:id="@+id/webview" android:layout_width="match_parent" android:layout_height="50dp" android:layout_alignParentTop="true" android:layout_centerHorizontal="true" />
Set up the WebView in the activity, in onCreate
// NOTE you MUST put a real ID in place of 'your_ad_id_here' // or you will not see an ad! WebView wv = (WebView) findViewById(R.id.webview); wv.getSettings().setJavaScriptEnabled(true); wv.setBackgroundColor(Color.TRANSPARENT); String html = "<html><body style='margin:0;padding:0;'><script type='text/javascript' src='https://ad.leadboltads.net/show_app_ad.js?section_id=your_ad_id_here'></script></body></html>"; wv.loadData(html, "text/html", "utf-8");
Set up the permissions in the manifest
<uses-permission android:name="android.permission.INTERNET"/>
It’s that easy!
That’s it! You can now deploy your app with ads without adding any SDK!
If you haven’t signed up for LeadBolt yet, sign up now. Then grab the project or just copy the code above, and your Android app is set up to show ads in a WebView!
I use html5 and jquery. Can webview be integrated to this?
If you area already displaying your HTML5 content, just make some space and add the JS snippet into your page. No webview needed. That’s the great thing about JS-based ad serving, it can be displayed anywhere that JavaScript can be used.
If you are looking to display HTML5 in a webview, I’m not sure how well that is supported. Older versions of Android do not support HTML5 much at all, so developers have used technologies like PhoneGap and other frameworks to create an app from HTML content.
I try put directly in HTML 5 phonegap application but it seem does not work…
What kind of problem do you see? Ensure that you have JavaScript enabled for the WebView, as it is needed to make the ad unit function.
Thanks. I just bought Android App Network. I only wanted to start with leadbolt for now. Would you know if leadbolt has some JS snippet that we can use? Really want to start this working asap.
The HTML snippet above in the article (on the “String html =” line) is all you need to display the ad. Just insert your own ad ID in the appropriate spot and you’re good to go!
Hi, when I show the webview in my app, if i click on the banner the link is not opened in a new window but in the banner area and gets cropped of course? Is this because the banner is in test mode? My app has not been approved yet by leadbolt. Dave
Never mind, it’s fixed. I am using a MoPub webview with leadbolt html banner code and inserting the code below to get the links to open in the default browser rather than in the banner frame. Now my app has no extra SDK’s, permissions to use 😛
…
oops, it seems your comment filter has stripped the code .
It did remove the code for some reason! I’m not sure why, but you can email it to me if you want me to post it. Glad you got it working!