If you are struggling to insert both banner and interstitial ads in android apps then follow my procedure. This is how I inserted both ads in my android application. The process is very simple and easy.
Step 1:
First of all, you need a google ads dependencies to run google ads in your android apps. So, let’s add google ads dependencies first.
implementation 'com.google.android.gms:play-services-ads:18.3.0'
Add the above line in your project build.gradle(Module: app) inside dependencies. After this sync your project. You will get the sync now button on the top right corner. Click and wait until the sync finished.
Step 2:
Now go activity.xml file and paste the following code in your layout.
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:layout_alignParentBottom="true">
</com.google.android.gms.ads.AdView>
within the same file add the following line in the header
xmlns:ads="http://schemas.android.com/apk/res-auto"
The activity will looks like the following
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<Button
android:layout_width="650dp"
android:layout_height="80dp"
android:text="@string/test"
android:id="@+id/btnpt1"
android:layout_marginTop="@dimen/vertical_gap"
android:background="@drawable/button_selector"
android:textSize="@dimen/text"
android:textColor="@color/white"
android:onClick="practicetest1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_id"
android:layout_alignParentBottom="true">
</com.google.android.gms.ads.AdView>
</RelativeLayout>
Step 3:
Check you manifest.xml has the following internet access permission or not. If you have not added before, its a time to add now.
<uses-permission android:name="android.permission.INTERNET" />
And Add your AdMob Application ID, it can be found in your Admob Application.
<manifest>
<application>
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
</application>
</manifest>
Step 4:
Now, Open the MainActivity.java file declare the following two lines.
public class MainActivity extends AppCompatActivity {
private InterstitialAd interstitial;
}
In the same MainActivity.java file, add the following code inside onCreate() method.
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
// Interstitial Ad
interstitial = new InterstitialAd(MainActivity.this);
interstitial.setAdUnitId(getString(R.string.admob_interstitial_id));
interstitial.loadAd(adRequest);
interstitial.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
}
});
Add the below function calling code before the last MainActivity.java closing curl.
public void displayInterstitial() {
if (interstitial.isLoaded()) {
interstitial.show();
}
}
Step 5:
Now, the last step is to define ad unit id in string.xml file
Add the below two code inside string.xml file.
<string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
<string name="admob_interstitial_id">ca-app-pub-3940256099942544/1033173712</string>
These are the Test Ads Unit Id. Before release, your app, make sure you change unit_id into your own.
That’s it.