### 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.  You will be redirect to a form. Click on *Add your app manually* tab. Then fill *App Name* field and choose the platform.  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.  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.  ##### 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.  ### 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/>
author | sgurdag | ||||||
---|---|---|---|---|---|---|---|
permlink | android-google-admob-sdk-integration | ||||||
category | utopian-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} | ||||||
created | 2018-02-23 20:37:39 | ||||||
last_update | 2018-02-24 14:34:21 | ||||||
depth | 0 | ||||||
children | 5 | ||||||
last_payout | 2018-03-02 20:37:39 | ||||||
cashout_time | 1969-12-31 23:59:59 | ||||||
total_payout_value | 0.022 HBD | ||||||
curator_payout_value | 0.000 HBD | ||||||
pending_payout_value | 0.000 HBD | ||||||
promoted | 0.000 HBD | ||||||
body_length | 6,169 | ||||||
author_reputation | 186,418,582 | ||||||
root_title | "Android - Google Admob SDK Integration" | ||||||
beneficiaries |
| ||||||
max_accepted_payout | 1,000,000.000 HBD | ||||||
percent_hbd | 10,000 | ||||||
post_id | 39,952,032 | ||||||
net_rshares | 5,561,871,453 | ||||||
author_curate_reward | "" |
voter | weight | wgt% | rshares | pct | time |
---|---|---|---|---|---|
st3llar | 0 | 619,183,249 | 3% | ||
sandan | 0 | 58,242,672 | 100% | ||
steemitstats | 0 | 2,062,372,999 | 5% | ||
claraquarius | 0 | 1,870,949,725 | 5% | ||
bardostyle07 | 0 | 338,219,215 | 100% | ||
ibrahimerkaplan | 0 | 612,903,593 | 100% |
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)**
author | amosbastian |
---|---|
permlink | re-sgurdag-android-google-admob-sdk-integration-20180224t135645340z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-24 13:56:45 |
last_update | 2018-02-24 13:56:45 |
depth | 1 |
children | 0 |
last_payout | 2018-03-03 13:56:45 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 619 |
author_reputation | 174,473,586,900,705 |
root_title | "Android - Google Admob SDK Integration" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 40,099,156 |
net_rshares | 0 |
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)**
author | cha0s0000 |
---|---|
permlink | re-sgurdag-android-google-admob-sdk-integration-20180224t143640244z |
category | utopian-io |
json_metadata | {"tags":["utopian-io"],"community":"utopian","app":"utopian/1.0.0"} |
created | 2018-02-24 14:36:33 |
last_update | 2018-02-24 14:36:33 |
depth | 1 |
children | 0 |
last_payout | 2018-03-03 14:36:33 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 474 |
author_reputation | 30,983,518,016,225 |
root_title | "Android - Google Admob SDK Integration" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 40,107,312 |
net_rshares | 0 |
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**!
author | steemitboard |
---|---|
permlink | steemitboard-notify-sgurdag-20190219t204841000z |
category | utopian-io |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2019-02-19 20:48:42 |
last_update | 2019-02-19 20:48:42 |
depth | 1 |
children | 0 |
last_payout | 2019-02-26 20:48:42 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 942 |
author_reputation | 38,975,615,169,260 |
root_title | "Android - Google Admob SDK Integration" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 80,115,299 |
net_rshares | 0 |
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!
author | steemitboard |
---|---|
permlink | steemitboard-notify-sgurdag-20200219t202527000z |
category | utopian-io |
json_metadata | {"image":["https://steemitboard.com/img/notify.png"]} |
created | 2020-02-19 20:25:27 |
last_update | 2020-02-19 20:25:27 |
depth | 1 |
children | 0 |
last_payout | 2020-02-26 20:25:27 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 1,078 |
author_reputation | 38,975,615,169,260 |
root_title | "Android - Google Admob SDK Integration" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 95,623,032 |
net_rshares | 0 |
@sgurdag, I like your contribution to open source project, so I upvote to support you.
author | steemitstats |
---|---|
permlink | 20180223t204540020z-post |
category | utopian-io |
json_metadata | {"tags":["utopian-io"]} |
created | 2018-02-23 20:46:00 |
last_update | 2018-02-23 20:46:00 |
depth | 1 |
children | 0 |
last_payout | 2018-03-02 20:46:00 |
cashout_time | 1969-12-31 23:59:59 |
total_payout_value | 0.000 HBD |
curator_payout_value | 0.000 HBD |
pending_payout_value | 0.000 HBD |
promoted | 0.000 HBD |
body_length | 86 |
author_reputation | 351,882,871,185 |
root_title | "Android - Google Admob SDK Integration" |
beneficiaries | [] |
max_accepted_payout | 1,000,000.000 HBD |
percent_hbd | 10,000 |
post_id | 39,953,386 |
net_rshares | 0 |