Banner & Interstitial Ads Using Android Studio

When we are using android studio and we need to insert ads such as full screen or banner ads where you want. Some of banner ads we can also use in navigation bar, fragments and main screen every else you want.

Let’s create your app to display Google admob banner and interstitial Ads on its Layout.

File: build.gradle file

Add the required Google Ads dependencies in build.gradle.file

STEP 1 :

dependencies
{
implementation fileTree(dir
: 'libs', include
: [ '*.jar' ])
implementation 'com.android.support:appcompat-v7:26.1.0'
//add here your Admob Ads Gradle
implementation'com.google.android.gms:play-services-ads:15.0.0'
}

STEP 2 :

Add the highlighted code to activity_main.xml to show banner ads.

activity_main.xml

<?xml version="1.0" encoding="utf-8>
<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"
tools:context=".MainActivity">


<TextView 
android:layout_width="wrap_content" android:layout_height="wrap_content" 
android:text="Hello World!" /> 

<!-- set Banner ad position in UI layout design --> 

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

STEP 3:

Add the highlighted code to show banner and Interstitial Code

private InterstitialAd interstitial;
private AdView mAdView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

////banner adview
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();
    }
  }
}

STEP 4 :

Add the Admob App id and banner Id to string.xml

Note: Don’t use your real Ad Unit ID while testing and never click on your own Ads it is against AdMob Policy.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="admob_app_id">
ca-app-pub-3940256099942544~3347511713</string>

<string name="admob_banner_id">
ca-app-pub-3940256099942544/6300978111</string>

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

STEP 5:

Then open the AndroidManifest.xml file and add internet option and something more please follow the highlighted copy and paste

<uses-permission android:name="android.permission.INTERNET" />
<application>
....
....
......
</activity>
<--admob-->
<meta-data android:name="com.google.android.gms.ads.APPLICATION_ID" 
android:value="@string/admob_app_id" /> 
</application>

That’s it, friends, this is the correct and simple way to insert Banner Ad and Interstitial Ad in your application. Just follow the above steps and copy paste code carefully at the correct location mentioned above.

if you found this article helpful please do comment below and share the article to support me. if you have any suggestion please feel free to comment below.

1 thought on “Banner & Interstitial Ads Using Android Studio”

  1. It is not my first time to pay a quick visit this web page, i am
    browsing this website dailly and get nice information from here daily.

    P.S. If you have a minute, would love your feedback on my new website re-design. You can find it
    by searching for “royal cbd” – no sweat if you can’t.

    Keep up the good work!

Leave a Comment

Your email address will not be published. Required fields are marked *