Integrating Google Admob in android app – { Simple Steps }

Please follow and like us:

integrating google admob android tutorial

Integrating Google Admob in android App [Example/Tutorial]

Google Admob is very popular and best ad network for advertising in mobile applications. With the help of google AdMob, developers can generate revenue from advertisement for their applications. Google AdMob gives many different types of advertisement options like smart banner advertises, full-screen interstitial advertises,  video advertises etc. so let’s integrating google AdMob in android app

Create Banner and Interstitial Advertise Unit Ids for integrating google AdMob in your android app

  1. Login/Signup to Google Admob with your Gmail account.
  2. Click on Monetise tab.
  3. click on “+ Monetise new app” button
  4. Add your app name and select platform
  5. Generate advertise unit ids for banner and interstitial ads.

these ads unit ids will help us in integrating google AdMob example in our android apps.

Your AndroidMainifest.xml file will add required permissions and Adview activity declaration for integrating google AdMob example

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.nkdroid.android.googleadmob">

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


    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme">


        <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" />

        <activity
            android:name=".SplashActivity"
            android:label="@string/app_name">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".MainActivity"
            android:label="@string/title_activity_main"></activity>
    </application>

</manifest>

add following dependency in application’s build.gradle file 

apply plugin: 'com.android.application'

android {
    compileSdkVersion 22
    buildToolsVersion "22.0.1"

    defaultConfig {
        applicationId "com.nkdroid.android.googleadmob"
        minSdkVersion 14
        targetSdkVersion 22
        versionCode 1
        versionName "1.0"
    }
    buildTypes {
        release {
            minifyEnabled false
            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
        }
    }
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.1'
    compile 'com.google.android.gms:play-services-ads:7.8.0'
}

color.xml in values folder for integrating google AdMob in android app will  look like

<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="primaryColor">#2196F3</color>
<color name="primaryColorDark">#1976D2</color>
<color name="colorAccent">#FF4081</color>
<color name="colorTextPrimary">#212121</color>
<color name="colorTextSecondary">#727272</color>
</resources>

style.xml in values folder for integrating google AdMob in android app will look like

<?xml version="1.0" encoding="utf-8"?>
<resources>

    <style name="AppTheme" parent="AppTheme.Base">

    </style>

    <style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

        <item name="colorPrimary">@color/primaryColor</item>
        <item name="colorPrimaryDark">@color/primaryColorDark</item>
        <item name="colorAccent">@color/colorAccent</item>
        <item name="android:textColorPrimary">@color/colorTextPrimary</item>
        <item name="android:textColorSecondary">@color/colorTextSecondary</item>

    </style>

</resources>

strings.xml in values folder for integrating google AdMob in android app will look like bellow, it contains google AdMob banner and interstitial advertise unit ids.

<resources>
    <string name="app_name">Google Admob Demo</string>
    <string name="hello_world">Hello world!</string>
    <string name="action_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    <string name="banner_ad_unit_id">your banner advertise id</string>
    <string name="full_screen_ad_unit_id">your interstial advertise id</string>

</resources>

activity_main.xml contains declaration of banner advertisement in XML file for integrating google AdMob tutorial in your android app

 

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"

    tools:context="com.nkdroid.android.googleadmob.MainActivity">

    <include layout="@layout/app_bar" />

    <com.google.android.gms.ads.AdView
        android:id="@+id/ad_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="@string/banner_ad_unit_id" />
</LinearLayout>

MainActivity.java file contains the main logic for integrating google AdMob example, here interstitial advertise will display only after it successfully loaded from server.

package com.nkdroid.android.googleadmob;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

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;


public class MainActivity extends AppCompatActivity {

    private Toolbar toolbar;
    private InterstitialAd mInterstitialAd;
    private AdView mAdView;

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

    private void setToolbar() {
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        if (toolbar != null) {
            toolbar.setTitle("Google Admob Demo");
            setSupportActionBar(toolbar);
        }
    }

    private void setupBannerAd() {
        mAdView = (AdView) findViewById(R.id.ad_view);
        AdRequest adRequest = new AdRequest.Builder().build();

        // Start loading the ad in the background.
        mAdView.loadAd(adRequest);


    }

    private void setupInterstialAd() {
        mInterstitialAd = new InterstitialAd(MainActivity.this);
        mInterstitialAd.setAdUnitId(getResources().getString(R.string.full_screen_ad_unit_id));

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

        mInterstitialAd.loadAd(adRequestFull);
        mInterstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                // Full screen advertise will show only after loading complete
                mInterstitialAd.show();
            }
        });
    }

}

Note: Generally developers load full-screen interstitial advertise for particular screen every time. This is bad thing and it may be block your google AdMob account. because this will effect when low internet connection users will try  to use your app but due to low internet sometimes your interstitial advertise loads parallel 2 times and fail to load in sequence.

So always load interstitial advertise for screen that comes rarely or when app will close.this will keep your google AdMob account alive.

Hope you liked our easy steps to integrating google AdMob example, if you find any difficulty in integrating google AdMob in android app then let me know via comments.

 

nirav kalola