create account

Android - Google Admob SDK Integration by sgurdag

View this thread on: hive.blogpeakd.comecency.com
· @sgurdag · (edited)
$0.02
Android - Google Admob SDK Integration
### 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
- [Google Admob](https://developers.google.com/admob/android/quick-start)

### 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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519388658/ub5w3n9jhn6hya2c6dno.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519391607/iwz8j9frrdbj7lbw28io.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519396584/lblz36f4ertpnvtiabcz.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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519402047/jc8mpnqrhmdxi7veih3n.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.

``` allprojects {
    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 .

``` <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        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 adView = new AdView(this);
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.AdRequest;
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](https://res.cloudinary.com/hpiynhbhq/image/upload/v1519417272/e60hif8hidsc8hbs27ah.png)

### Curriculum

- [Android tutorial](https://developers.google.com/admob/android/quick-start)

   

<br /><hr/><em>Posted on <a href="https://utopian.io/utopian-io/@sgurdag/android-google-admob-sdk-integration">Utopian.io -  Rewarding Open Source Contributors</a></em><hr/>
👍  , , , , ,
properties (23)
authorsgurdag
permlinkandroid-google-admob-sdk-integration
categoryutopian-io
json_metadata{"community":"utopian","app":"utopian/1.0.0","format":"markdown","repository":{"id":30143553,"name":"admob-android","full_name":"massimocarli/admob-android","html_url":"https://github.com/massimocarli/admob-android","fork":false,"owner":{"login":"massimocarli"}},"pullRequests":[],"platform":"github","type":"tutorials","tags":["utopian-io","android","ad","monitising","admob"],"users":["dimen","string"],"links":["https://developers.google.com/admob/android/quick-start","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519388658/ub5w3n9jhn6hya2c6dno.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519391607/iwz8j9frrdbj7lbw28io.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519396584/lblz36f4ertpnvtiabcz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519402047/jc8mpnqrhmdxi7veih3n.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519417272/e60hif8hidsc8hbs27ah.png"],"image":["https://res.cloudinary.com/hpiynhbhq/image/upload/v1519388658/ub5w3n9jhn6hya2c6dno.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519391607/iwz8j9frrdbj7lbw28io.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519396584/lblz36f4ertpnvtiabcz.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519402047/jc8mpnqrhmdxi7veih3n.png","https://res.cloudinary.com/hpiynhbhq/image/upload/v1519417272/e60hif8hidsc8hbs27ah.png"],"moderator":{"account":"cha0s0000","time":"2018-02-24T14:34:21.708Z","flagged":true,"reviewed":false,"pending":false},"questions":[],"score":0}
created2018-02-23 20:37:39
last_update2018-02-24 14:34:21
depth0
children5
last_payout2018-03-02 20:37:39
cashout_time1969-12-31 23:59:59
total_payout_value0.022 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length6,169
author_reputation186,418,582
root_title"Android - Google Admob SDK Integration"
beneficiaries
0.
accountutopian.pay
weight2,500
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id39,952,032
net_rshares5,561,871,453
author_curate_reward""
vote details (6)
@amosbastian ·
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/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](https://developers.google.com/admob/android/quick-start) and [here](https://developers.google.com/admob/android/banner).

You can contact us on [Discord](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authoramosbastian
permlinkre-sgurdag-android-google-admob-sdk-integration-20180224t135645340z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-24 13:56:45
last_update2018-02-24 13:56:45
depth1
children0
last_payout2018-03-03 13:56:45
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length619
author_reputation174,473,586,900,705
root_title"Android - Google Admob SDK Integration"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,099,156
net_rshares0
@cha0s0000 ·
Your contribution cannot be approved because it does not follow the [Utopian Rules](https://utopian.io/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](https://discord.gg/uTyJkNm).
**[[utopian-moderator]](https://utopian.io/moderators)**
properties (22)
authorcha0s0000
permlinkre-sgurdag-android-google-admob-sdk-integration-20180224t143640244z
categoryutopian-io
json_metadata{"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"}
created2018-02-24 14:36:33
last_update2018-02-24 14:36:33
depth1
children0
last_payout2018-03-03 14:36:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length474
author_reputation30,983,518,016,225
root_title"Android - Google Admob SDK Integration"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id40,107,312
net_rshares0
@steemitboard ·
Congratulations @sgurdag! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@sgurdag/birthday1.png</td><td>Happy Birthday! - You are on the Steem blockchain for 1 year!</td></tr></table>

<sub>_[Click here to view your Board](https://steemitboard.com/@sgurdag)_</sub>


**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/valentine/@steemitboard/valentine-challenge-love-is-in-the-air"><img src="https://steemitimages.com/64x128/http://i.cubeupload.com/LvDzr5.png"></a></td><td><a href="https://steemit.com/valentine/@steemitboard/valentine-challenge-love-is-in-the-air">Valentine challenge - Love is in the air!</a></td></tr></table>

> Support [SteemitBoard's project](https://steemit.com/@steemitboard)! **[Vote for its witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1)** and **get one more award**!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-sgurdag-20190219t204841000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-02-19 20:48:42
last_update2019-02-19 20:48:42
depth1
children0
last_payout2019-02-26 20:48:42
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length942
author_reputation38,975,615,169,260
root_title"Android - Google Admob SDK Integration"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,115,299
net_rshares0
@steemitboard ·
Congratulations @sgurdag! You received a personal award!

<table><tr><td>https://steemitimages.com/70x70/http://steemitboard.com/@sgurdag/birthday2.png</td><td>Happy Birthday! - You are on the Steem blockchain for 2 years!</td></tr></table>

<sub>_You can view [your badges on your Steem Board](https://steemitboard.com/@sgurdag) and compare to others on the [Steem Ranking](https://steemitboard.com/ranking/index.php?name=sgurdag)_</sub>


**Do not miss the last post from @steemitboard:**
<table><tr><td><a href="https://steemit.com/steemitboard/@steemitboard/valentine-s-day-challenge-give-a-badge-to-your-beloved"><img src="https://steemitimages.com/64x128/http://i.cubeupload.com/LvDzr5.png"></a></td><td><a href="https://steemit.com/steemitboard/@steemitboard/valentine-s-day-challenge-give-a-badge-to-your-beloved">Valentine's day challenge - Give a badge to your beloved!</a></td></tr></table>

###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) to get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-sgurdag-20200219t202527000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2020-02-19 20:25:27
last_update2020-02-19 20:25:27
depth1
children0
last_payout2020-02-26 20:25:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,078
author_reputation38,975,615,169,260
root_title"Android - Google Admob SDK Integration"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id95,623,032
net_rshares0
@steemitstats ·
@sgurdag, I like your contribution to open source project, so I upvote to support you.
properties (22)
authorsteemitstats
permlink20180223t204540020z-post
categoryutopian-io
json_metadata{"tags":["utopian-io"]}
created2018-02-23 20:46:00
last_update2018-02-23 20:46:00
depth1
children0
last_payout2018-03-02 20:46:00
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length86
author_reputation351,882,871,185
root_title"Android - Google Admob SDK Integration"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id39,953,386
net_rshares0