Use LeadBolt to show ads in an Android WebView

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!

Posted in Android Tutorials permalink

About ProjectJourneyman

I am a software engineer that escaped the cubicle world at a large company to go solo with Android app development. My attention to detail and quality applies both to my apps and to my research on how to make money with Android. Now that I have the freedom to work on my own projects, I am documenting my efforts in the hopes that it will help other current or aspiring independent Android developers get the income they desire.

Comments

Use LeadBolt to show ads in an Android WebView — 10 Comments

    • 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.

  1. 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!

  2. 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

  3. 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 😛