Android - Google Admob SDK Integration

in #utopian-io8 years ago (edited)

What Will I Learn?

  • How to integrate Google Admob SDK to an Android app.
  • Showing Banner Ads

Requirements

  • Android Studio
  • Intermediate level code knowledge

Difficulty

  • Intermediate

Useful Links

Tutorial Contents

Google Admob is a platform that can be monetised an app through Google Ads. You will be shown how to integrate Google Admob SDK to an application step by step.

Creating A New App In Google Admob Dashboard

First thing which is needed to do is creating an ad unit in Google Admob dashboard. To create an ad unit , you need to sign in to your AdMob account at https://apps.admob.com. After signing in , click Monitize tab then click +Monitise New App tab at Admob dashboard.

image.png

You will be redirect to a form. Click on Add your app manually tab. Then fill App Name field and choose the platform.

Screen Shot 2018-02-23 at 16.11.59.png

Click Add app button for creating Ad Unit.

Creating A New Ad Unit

An ad unit can be define as model of each ad. An Ad Unit ID is given after it is created. Then ads which will be shown be called with this IDs from app.

Screen Shot 2018-02-23 at 17.35.21.png

Following details needs to be provided in this screen ;

Ad Type : Choose which type of ads are wanted to be shown in Ad unit.

Automatic refresh: Refreshment option of ad. It can be completely disable or set up to 120 seconds. Recommended refresh rate is from 45 to 60 seconds.

Text ad style: Ad text style can be customised through this option.

Ad unit name : A unique name needs to be assigned to the ad unit.

After providing the details you will be given an Ad Unit ID after clicking Save button.

image.png

Integration of the SDK

It is needed to integrate Google Mobile Ads SDK as dependency for showing ads in app .
Open app's build.gradle file and add this code in dependencies section. Then click Sync to synchronise the project.

compile 'com.google.android.gms:play-services-ads:11.8.0'

This code needs to be added in project-level build.gradle file.

    repositories {
        jcenter()
        maven {
            url "https://maven.google.com"
        }
    }
}
Initialisation of SDK

Mobile Ads SDK has to be initialised with Admob App ID before loading ads. It only needs to be done once. So, ideally it can be at app launch.

Here's an example of how to call the initialize() method in an Activity:

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

        MobileAds.initialize(this, "YOUR_ADMOB_APP_ID");
    }
    ...
}

Showing Banner Ads

The first step to show a banner ad is to place an AdView to the layout of Activity or Fragment which is shown .

        xmlns:tools="http://schemas.android.com/tools"
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        android:paddingBottom="@dimen/activity_vertical_margin"
        tools:context=".MainActivity">

        <TextView android:text="@string/hello_world"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <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="YOUR_AD_UNIT_ID">
        </com.google.android.gms.ads.AdView>

</RelativeLayout>

Note : If more than one banner will be shown in different activities or fragments , It is necessary to define an Ad Unit for each.

AdView can be created programmatically as well :

adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("YOUR_AD_UNIT_ID");
// TODO: Add adView to your view hierarchy.

Note : It is better be tested with test Ad Unit ID : ca-app-pub-3940256099942544/6300978111

Loading an ad

Loading an ad is done with the loadAd() method in the AdView class. It takes an AdRequest parameter, which holds runtime information (such as targeting info) about a single ad request.

import com.google.android.gms.ads.AdView;

public class MainActivity extends AppCompatActivity {
    private AdView mAdView;

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

        MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");

        mAdView = findViewById(R.id.adView);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    }
}

The ad is ready. It will be shown when the app is run.

image.png

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

@sgurdag, I like your contribution to open source project, so I upvote to support you.

Your contribution cannot be approved because it does not follow the Utopian Rules.

Hi, these are the reasons your contribution was rejected

  • The linked repository is wrong. It's a repository for "AdMob example for Google Play Services Book" and hasn't been updated for more than 3 years.
  • All the code in your tutorial is simply copied from here and here.

You can contact us on Discord.
[utopian-moderator]

Your contribution cannot be approved because it does not follow the Utopian Rules.
Utopian rule

  • Tutorials must be technical instructions that teach non-trivial aspects of an Open Source project

Explanation

  • admob-android is not the qualified open source project because of lacking of license file and so on

You can contact us on Discord.
[utopian-moderator]

Congratulations @sgurdag! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 1 year!

Click here to view your Board

Do not miss the last post from @steemitboard:

Valentine challenge - Love is in the air!

Support SteemitBoard's project! Vote for its witness and get one more award!

Congratulations @sgurdag! You received a personal award!

Happy Birthday! - You are on the Steem blockchain for 2 years!

You can view your badges on your Steem Board and compare to others on the Steem Ranking

Do not miss the last post from @steemitboard:

Valentine's day challenge - Give a badge to your beloved!
Vote for @Steemitboard as a witness to get one more award and increased upvotes!