How To Add Admob Ads in Android App Using Android Studio

 Add Admob Ads In Android App – Admob Integration

Today, we will learn how to add AdMob ads in the android app using the android studio. So, I will discuss Admob banner & Interstitial Ads. It’s an easy method of AdMob integration in the android application. By using this method anyone can add google Admob ads on their own Android app. So, We will add Admob ads on our Android project How to Convert a Website To Android app. Therefore, we will work on this android app.

 1. ACCESS_NETWORK_STATE  in Android Manifest

First of all, we will add access_network_state permission in androidmanifest.xml. So simply open this file in the android studio and add this permission to the bottom of internet permission.

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

2.  activity_main.xml:

Now, you need to add AdMob banner ad code in activity_main.xml. Simply, place the following code at the end of the code before the</ RelativeLayout>.

<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_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">
</com.google.android.gms.ads.AdView>

Seems like the screenshot

add admob in android app

3. string.xml for AdMob ads : 

You need to add 2 string in string.xml file to place the AdMob pub ID for banner & Interstitial Ads. Therefore, add these following string in your string file. Note: you need to replace your own AdMob pub ID after the app working properly.

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

4. Build.gradle (module):

To run the AdMob ads we need google play services ads dependency, So please add the following dependency in the dependencies section of the build.Gradle (module) file.

implementation 'com.google.android.gms:play-services-ads:18.3.0'

Seems like this screenshot

admob-dependency-in-build.gradle

5. MainActivity.java for Admob banner and Interstitial Ads: 

Finally, We need the MainActivity.java code to run the AdMob on our Android app. So, Friends, we put the following codes in the main activity.java

First of all, in this file, we need 4 import at the top of MainActivity.java. Therefore, add these 4 import at the top of the file.

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

Seems like this screenshot

integration-admob-in-android-app-mainactivity

Now, we will add 2 more codes below the public class MainActivity extends AppCompat which shown in the above screenshot.

 private InterstitialAd interstitial;
AdView mAdView;

In final you need to add the following code below the  setContentView(R.layout.activity_main);

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

Seems like this screenshot

main-activity-java-code-to-admob-ads

So, friends, now you can run your app in android mobile or android emulator. I am sure you will be happy by following our guidelines. Note: Real ads code only works when u published your app on play store.

In Conclusion:

I hope you will successfully add  AdMob in your Android app by following our guidelines. If you have any problem, please share with us. We will try to reply to your comment within 2 business days.