How To Add Admob Ads in Android App – Admob Integration Tutorial

 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 android application. By using this method anyone can add google Admob ads on their own Android app. In our previous post, we have to make an application by using any website. 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 in the bottom of internet permission.

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

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.

compile 'com.google.android.gms:play-services-ads:8.4.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 ads on our Android app. So, Friends, we put 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 the shown in above screenshot.

                     InterstitialAd mInterstitialAd;
                    private InterstitialAd interstitial;

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 guideline.

In Conclusion:

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

x

Check Also

How To Make Flashlight Application in Android Studio

 Make Flashlight Application In Android : Today, Our post is How to make flashlight app ...