sagar chaudhary
sagar chaudhary

Reputation: 33

Webview App not showing admob banner ads

I am creating a web view android app in which i want to add ad-mob interstitial and banner ads, i have already implemented codes for that, and they are working fine on my test app but when i try the same codes in my web-view app only interstitial ads works. can someone please help me with right codes. following are the codes

build.gradle

compile 'com.google.android.gms:play-services-ads:9.2.0'

activity_main.xml

<WebView
    android:id="@+id/activity_main_webview"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:visibility="gone"/>


     <com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

MainActivity.java

    InterstitialAd mInterstitialAd;
private InterstitialAd interstitial;



 AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);

// Prepare the Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
// Insert the Ad Unit ID
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));

interstitial.loadAd(adRequest);
// Prepare an Interstitial Ad Listener
interstitial.setAdListener(new AdListener() {
    public void onAdLoaded() {
        // Call displayInterstitial() function
        displayInterstitial();
    }
});

public void displayInterstitial() {
// If Ads are loaded, show Interstitial else show nothing.
if (interstitial.isLoaded()) {
interstitial.show();
}
}

strings.xml

<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="admob_interstitial_id">ca-app-pub-3940256099942544/1033173712</string>

Upvotes: 0

Views: 2331

Answers (2)

Mohit Madaan
Mohit Madaan

Reputation: 469

Try with this code. It's working.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:ads="http://schemas.android.com/apk/res-auto">
<WebView android:id="@+id/webView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent"/>
<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

and in java file

WebView webView = (WebView) findViewById(R.id.webView1);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.loadUrl(Url);
    AdView mAdView = (AdView) findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

Upvotes: 1

Itiel Maimon
Itiel Maimon

Reputation: 854

Try to use CoordinatorLayout as your root view:

<android.support.design.widget.CoordinatorLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:fitsSystemWindows="true"
  tools:context="">


   // yout stuff here...

</android.support.design.widget.CoordinatorLayout>

Upvotes: 0

Related Questions