Hi all,
I’ve seen a few people mention that the HTML LeadBolt App Wall is difficult to integrate. I definitely found it a bit confusing at first. I wrote my own Activity as a wrapper for the HTML ads. Since it ended up involving a few WebView quirks, I thought I’d share it with you guys in case it helps your integration.
Here’s the Activity code:
[java]
import java.util.Random;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class AdViewLBWall extends Activity {
protected static final String TAG = "AdViewLBWall";
private ProgressDialog progress;
//Whether the first page loaded successfully
private static boolean initialLoadFinished = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.leadbolt_app_wall);
//Get Webview
WebView adWebView = (WebView) findViewById(R.id.adWebView);
//Set callback on error
adWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d(TAG, "URL: "+url);
// If this is HTTP or HTTPS, load as normal
if(url.startsWith("http")) {
//Don't override
return false;
} else {
// Otherwise, this might be a market:// or mailto:// URL,
// so launch another Activity that handles URLs
try {
//Safely launch the intent
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
startActivity(intent);
//Once we've left this application, no need to keep the ad visible
finish();
return true;
} catch (Exception e) {
//Couldn't launch intent on this device
e.printStackTrace();
//Let the user know
Toast.makeText(AdViewLBWall.this, "Not supported on your device.", 1000).show();
//If the URL's not working, just quit this ad display
//No point annoying the user further by letting them try again
finish();
return true;
}
}
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Log.i(TAG, "WebView failed to load. Error code:" + errorCode);
//If this was the first page, and failed to load, we should die
//Because there's no way the user can do anything
if(initialLoadFinished==false) {
//Hide webview
view.setVisibility(View.INVISIBLE);
hideProgress();
super.onReceivedError(view, errorCode, description, failingUrl);
//Cancel this screen
finish();
}
}
@Override
public void onPageFinished(WebView view, String url) {
Log.d(TAG, "WebView onPageFinished");
hideProgress();
//We've loaded the first page
initialLoadFinished = true;
}
});
//Select url to display
String url = "INSERT_YOUR_APPWALL_URL_HERE";
//Load data
adWebView.getSettings().setJavaScriptEnabled(true);
adWebView.setInitialScale(1);
adWebView.setBackgroundColor(0);
adWebView.loadUrl(url);
//Show progress bar
showProgress();
}
private void showProgress() {
if(progress==null) {
progress = new ProgressDialog(this);
progress.setMessage("Loading...");
}
progress.show();
}
private void hideProgress() {
if(progress!=null && progress.isShowing()) {
progress.dismiss();
}
}
@Override
public void onPause() {
super.onPause();
//Try to prevent context leaks
hideProgress();
}
}
[/java]
And here’s the accompanying XML layout:
[xml]
<?xml version=“1.0” encoding=“utf-8”?>
<LinearLayout xmlns:android=“http://schemas.android.com/apk/res/android”
android:id="@+id/mainLayout"
android:layout_width=“fill_parent”
android:layout_height=“fill_parent”
android:gravity=“center”
android:orientation=“vertical” >
<WebView
android:id="@+id/adWebView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
[/xml]
There are a few tricks in there, especially the part which overrides the URL handler (this makes sure market:// URLs are handled correctly). I hope this code will help people who are starting out with LeadBolt to integrate the HTML App Wall more easily. All you have to do is start a new Activity with this class:
[java]
startActivity(this, AdViewLBWall.class);
[/java]
Of course, you could also use the LeadBolt SDK which includes a native App Wall. But I prefer using this method - it gives me more control over displaying a progress dialog while loading, and allows me to potentially pre-cache the HTML itself in future.
Any suggestions for improvement are welcome - I’m sure there are some parts which could be tidied up Just thought I’d throw it out there anyway, in case somebody can use it.
Cheers,
David