Quick Start Integrated Google Mobile Admob in Android

Sovary May 24, 2022 564
5 minutes read

The first step in displaying ads and earning revenue is to integrate the Google Mobile Ads SDK into your app. You can select an ad type (such as banner, interstitail, native or rewarded video) and proceed with the implementation process.

Google Admob have provide different ads formats, so that you can decided to choose which one is suitable for your app. The following are ad formats available for Android applications.

 To generate income by integrating Google AdMob advertising in Android applications, There are following steps below.

  1. Register Admob Account
  2. Choose ad format and Create ad Unit
  3. Integrate Ads SDK into app
  4. Publish App into Playstore

Today I will show you very quick how to config Admob Ads SDK at first place before go to deep implement each of ad type.

This article is well test in Google Mobile Ads SDK version 20.0.0 and 20.6.0 which contained many breaking changes from early version.

Prerequisites

  • Android Studio
  • Internet connection
  • Mobile Ads SDK 20.6.0 or higher.
  • Sign Up Google Admob Account
  • minSdkVersion 16+
  • compileSdkVersion 28+

In development mode please always use test ads rather than production ads (your admob ad). Failure to do so can lead to suspension of your account.

Sample Admob App ID: ca-app-pub-3940256099942544~3347511713 
Ad Format Sample Unit ID (Safe)
Banner ca-app-pub-3940256099942544/6300978111
Interstitial ca-app-pub-3940256099942544/1033173712
Interstitial Video ca-app-pub-3940256099942544/8691691433
Rewarded ca-app-pub-3940256099942544/5224354917

Make sure you replace it with your own ad unit ID before publishing your app to Playstore. I do recommend you to place those ID in strings.xml for easy switch to production mode.

Open app -> res -> values -> strings.xml and paste below snippet.

<string name="AppID">ca-app-pub-3940256099942544~3347511713</string>
<string name="AdFullScreen">ca-app-pub-3940256099942544/1033173712</string>
<string name="AdBanner">ca-app-pub-3940256099942544/6300978111</string>

Insert Config in AndroidManifest

Add the <meta-data> element to your Android App AndroidManifest.xml file to insert your AdMob App ID where you can find in your Admob Account.

The file found under app -> manifests -> AndroidManifest.xml.

<manifest>
    <application>
        <!-- Sample AdMob app ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="@string/AppID"/>
    </application>
</manifest>

@string/AppID is your App ID that we already place in strings.xml. Again for testing phase I do recommend to use sample App ID.

To retrieved data ad from Google Ad inventory we need internet connection and Android need permission to acess the internet. To do this we add an element below out side application element

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

Import and config Mobile Ads SDK

To able use any objects and xml view in Mobile Ads dependency, you need to download dependency library to your app's module (app-level) in build.gradle file.

Open Gradle Scripts -> build.gradle(Module: AppName.app)

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

After modified in that file, it will show a message to sync project click on that and pull the dependency. Don't forget internet connection.

Then add in top level build file with following snippet. The below is for Project's module (project-level) in build.gradle file. 

Open Gradle Scripts -> build.gradle(Project: AppName)

dependencies {
    classpath "com.android.tools.build:gradle:4.2.1"
    classpath 'com.google.gms:google-services:4.3.10' //Add this line
    // NOTE: Do not place your application dependencies here; they belong
    // in the individual module build.gradle files
}

Initialize Mobile Ads

To load any type of ad in our app we need to initialize Mobiles Ads SDK.

Open main activity java file MainActivity.java in app -> java -> package(com.cambotutorial.sovary.adtest) -> MainActivity.java.

package com.cambotutorial.sovary.adtest
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //initialize MobileAds SDK
        //Call this only one time for every ad type format
        MobileAds.initialize(this, new OnInitializationCompleteListener() {
            @Override
            public void onInitializationComplete(InitializationStatus initializationStatus) {
            }
        });
    }
}

We have finished importing and config Mobile Ads SDK and now are you good to go implmenting any type of ads in our application.

Choose Ad Format

Google AdMob have different type of ad formats for the best fits our app’s user experience. If you don't know which one is best for you check of each ad format the below.

Banner Ads

The retangle ad format contain image or text. This format type usually implement on bottom or top of app's UI. it can be refresh new ads automatically after afew minutes. It is great place to start for fresh beginner.

To learn more about Banner Ads implementation in Android applications, I have wrote an article Android Banner Ads with Examples.

Interstitial Ads

The full screen ad format cover UI of app. It's display overlay the app until user close. They're best used at natural pauses in the flow of an app's execution, such as in between each Activity layout transition or just after completing a task.

To learn more about Interstitial Ads implementation in Android applications, I have wrote an article Android Interstitial Ads with Examples.

Rewarded Ads

This ad format is full-screen video ads and it rewards the users for completely watching the ads. It usually implement in game. 

To learn more about Rewarded Ads implementation in Android applications, I have wrote an article Android Rewarded Ads with Examples.

Native Ads

Ads can be customised to complimen the appearance of your app. You can choose how and where they placed in your layout, so the layout matche with your app's style.

To learn more about Native Ads implementation in Android applications, I have wrote an article Android Native Ads with Examples.

You might Also Like:

Android  Java  Admob 
Author

Founder of CamboTutorial.com, I am happy to share my knowledge related to programming that can help other people. I love write tutorial related to PHP, Laravel, Python, Java, Android Developement, all published post are make simple and easy to understand for beginner. Follow him    

Search