Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Integrating Admob Codelab

Integrating Admob Codelab

Slides from my talk at GDG New Delhi on Integrating Admob in your Android application

Kshitij Aggarwal

March 19, 2016
Tweet

More Decks by Kshitij Aggarwal

Other Decks in Programming

Transcript

  1. Why AdMob • It’s all about the Money honey •

    Earn where you cannot sell • Integrated in the whole Google ecosystem
  2. Let’s Begin 1. Create an Android Project 2. Set minSDK

    = 14 or 15 3. Add an Empty Activity
  3. Update build.gradle dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile

    'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.2.1' compile 'com.google.android.gms:play-services-ads:8.4.0' }
  4. Getting Ad Unit Id 1. Visit apps.admob.com 2. Login with

    Google Account 3. Fill out the info 4. Go to ‘Monetize’ tab 5. Click ‘Add App Manually’ 6. Fill the details
  5. Banner Ad via XML <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools"

    xmlns:ads="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.gdgnd.admobcodelab.MainActivity"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" ads:adSize="SMART_BANNER" ads:adUnitId="@string/banner_ad_unit_id"> </com.google.android.gms.ads.AdView> </RelativeLayout>
  6. Banner Ad Activity Code package com.gdgnd.admobcodelab; import android.os.Bundle; import android.support.v7.app.AppCompatActivity;

    import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); AdView mAdView = (AdView) findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder().build(); mAdView.loadAd(adRequest); } }
  7. Interstitial Ads via Activity private InterstitialAd newInterstitialAd() { InterstitialAd interstitialAd

    = new InterstitialAd(this); interstitialAd.setAdUnitId(getString(R.string.interstitial_ad_unit_id)); interstitialAd.setAdListener(new AdListener() { @Override public void onAdLoaded() { interstitialAd.show(); } @Override public void onAdFailedToLoad(int errorCode) { // Move on with the app } @Override public void onAdClosed() { // Proceed to the next level. } }); return interstitialAd; }
  8. Interstitial Ads via Activity ..contd private void loadInterstitial() { mNextLevelButton.setEnabled(false);

    AdRequest adRequest = new AdRequest.Builder().setRequestAgent("android_studio:ad_template").build(); mInterstitialAd.loadAd(adRequest); }
  9. Ad Etiquettes 1. Don’t surprise the user with Interstitials 2.

    Don’t add Interstitials at every 3rd click 3. Keep Banner Ads either on Top or Bottom 4. If Ad doesn't load, then move on 5. Don’t click on live ads in test apps