-4

I just want interstitial ad in my main activity anyhow

    private void initAdmob() {
        if (isAdmobEnable) {
            adView = new AdView(this);
            adView.setAdSize(AdSize.BANNER);
            adView.setAdUnitId(MY_AD_UNIT_ID);

            RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
                    RelativeLayout.LayoutParams.MATCH_PARENT,
                    RelativeLayout.LayoutParams.WRAP_CONTENT);
        //  p.addRule(RelativeLayout.BELOW, webviewProgress.getId());
             p.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            parentView.addView(adView, p);

            AdRequest adRequest = new AdRequest.Builder().build();

            // Start loading the ad in the background.
            adView.loadAd(adRequest);
        }
    }
Zoe
  • 27,060
  • 21
  • 118
  • 148
Niu Zest
  • 1
  • 2

2 Answers2

0

Well since your code is extremely large I'll put a scenario to how to do it and you can guide with my code.

1. First of all make sure you have the code of Google as services in your build.gradle

compile 'com.google.android.gms:play-services:7.0.0'

2. Add those lines on your manifest.xml

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<application>
<meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version"/>
<activity android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

3. On your Activity where you want to add the ad view should contains this code :

private InterstitialAd mInterstitialAd;
mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
        mInterstitialAd.show();
} 
else {
       if (!mInterstitialAd.isLoaded() && !mInterstitialAd.isLoaded()) {
        AdRequest adRequest = new AdRequest.Builder().build();
        mInterstitialAd.loadAd(adRequest);
       }  
}

If you want to add it programmatically you should include your WebView on a LinearLayout (easier) and then add it doing this :

private AdView adView;
adView = new AdView(this, AdSize.INTERSTITIAL, getString(R.string.ad_unit_id));
LinearLayout root = (LinearLayout)findViewById(R.id.your_main_LinearLayout);
root.addView(adView);
adView.loadAd(new AdRequest());

And if you don't want to do it programmatically just add the AdView on your xml file below the WebView

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="false"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="INTERSTITIAL"
    ads:adUnitId="@string/banner_ad_unit_id">   
 </com.google.android.gms.ads.AdView>
Skizo-ozᴉʞS ツ
  • 19,464
  • 18
  • 81
  • 148
0

import com.google.android.gms.ads.AdListener;

private InterstitialAd interstitial;





     interstitial = new InterstitialAd(WebViewActivity.this);
                // Insert the Ad Unit ID
                interstitial.setAdUnitId("ca-app-pub-6466988621230720/20102112499");


    // Request for Ads
    AdRequest adRequest = new AdRequest.Builder()

    // Add a test device to show Test Ads
    // .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    // .addTestDevice("0A8B6312AD2047B3E0951BFB32026553")
            .build();

    // Load ads into Banner Ads
    adView.loadAd(adRequest);

    // Load ads into Interstitial Ads
    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();
    }
}
Niu Zest
  • 1
  • 2