'AdMob ads with a WebView in the same layout?
I'm trying to implement AdMob ads with a WebView. For the life of me, I can't get anything to show, even in the logs. It's almost like the ads are not being called at all - my AdMob account shows no requests. At first I was thinking the WebView was just taking up all the space in the layout, but now I'm second guessing myself, because I'm not seeing any LogCat output relating to ads.
Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="[myIDishere]"
ads:adSize="BANNER"
ads:testDevices="[myhashishere]"
ads:loadAdOnCreate="true"/>
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
And then my main activity:
public class MainActivity extends Activity {
//StartServicesCheck
private boolean checkPlayServices() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
showErrorDialog(status);
} else {
Toast.makeText(this, "This device is not supported.",
Toast.LENGTH_LONG).show();
finish();
}
return false;
}
return true;
}
void showErrorDialog(int code) {
GooglePlayServicesUtil.getErrorDialog(code, this,
REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
}
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_RECOVER_PLAY_SERVICES:
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
//EndServicesCheck
private WebView mWebview ;
@Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
public class BannerExample extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Look up the AdView as a resource and load a request.
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("[webpagehere]");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
}
Do I have something wrong in my coding itself? I'm kind of leaning that way because I'm not seeing requests or log output.
Solution 1:[1]
Change the WebView to the following. Your AdView is being hidden by the WebView:
<WebView
android:id="@+id/webView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
I would also suggest to set the width of AdView to match_parent and adSize as "SMART_BANNER"
SMART_BANNER ads automatically resize themselves according to the orientation and width od the device.
EDIT
I just went through your code. It had many small mistakes. change it to the following:
public class MainActivity extends Activity {
//StartServicesCheck
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
mWebview = (WebView) findViewById(R.id.webview);
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.getSettings().setBuiltInZoomControls(true);
if(android.os.Build.VERSION.SDK_INT >= 11) {
mWebview.getSettings().setDisplayZoomControls(false);
}
final Activity activity = this;
mWebview.setWebViewClient(new WebViewClient() {
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(activity, description, Toast.LENGTH_SHORT).show();
}
});
mWebview .loadUrl("[webpagehere]");
setContentView(mWebview );
mWebview.setWebViewClient(new WebViewClient(){
@Override
public void onPageFinished(WebView view, String url) {
// TODO Auto-generated method stub
super.onPageFinished(view, url);
mWebview.scrollTo(681, 100);
}
}
);
}
private boolean checkPlayServices() {
int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(this);
if (status != ConnectionResult.SUCCESS) {
if (GooglePlayServicesUtil.isUserRecoverableError(status)) {
showErrorDialog(status);
} else {
Toast.makeText(this, "This device is not supported.",
Toast.LENGTH_LONG).show();
finish();
}
return false;
}
return true;
}
void showErrorDialog(int code) {
GooglePlayServicesUtil.getErrorDialog(code, this,
REQUEST_CODE_RECOVER_PLAY_SERVICES).show();
}
static final int REQUEST_CODE_RECOVER_PLAY_SERVICES = 1001;
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case REQUEST_CODE_RECOVER_PLAY_SERVICES:
if (resultCode == RESULT_CANCELED) {
Toast.makeText(this, "Google Play Services must be installed.",
Toast.LENGTH_SHORT).show();
finish();
}
return;
}
super.onActivityResult(requestCode, resultCode, data);
}
//EndServicesCheck
private WebView mWebview ;
@Override
public void onBackPressed() {
if(mWebview.canGoBack() == true){
mWebview.goBack();
}else{
super.onBackPressed();
}
}
}
PS: Sorry for the messed up indentation :P
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | ZP007 |
