Last updated on July 31st, 2016 at 11:16 am
Google allow android developer to implement google ads into their application using AdMob. AdMob is a platform to display google ads on mobile application. In this post i explain you how to Implement AdMob in your Android application and generate revenue from your application and motorize your earning from application.
GETTING STARTED
Sign up as an Admob Publisher here. Log in to your dashboard that shows your revenues and estimated earnings.
Implementing AdMob in android Application Code
Step 1. Create new android project in Android Studio or Eclipse.
Step 2. Install Google Play service and Google Repository before you start implementing admob in your project.
Step 3. Add Google Play services in your project. If you are using Android Studio you need to add dependencies in your build.gradle as shown bellow
apply plugin: 'com.android.application' android { compileSdkVersion 21 buildToolsVersion "20.0.0" defaultConfig { applicationId "com.addmob" minSdkVersion 14 targetSdkVersion 21 versionCode 1 versionName "1.0" } buildTypes { release { runProguard false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) compile 'com.google.android.gms:play-services:6.1.11' }
Follow this link to find latest version of Play service : http://gradleplease.appspot.com/
Step 4. Now open your activity file and implement AdView in activity to display ads.
<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" android:paddingLeft="0dp" android:paddingRight="0dp" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MyActivity" android:id="@+id/relative_l"> <com.google.android.gms.ads.AdView android:id="@+id/adView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_centerHorizontal="true" ads:adSize="BANNER" ads:adUnitId="@string/banner_ad_unit_id" /> </RelativeLayout>
Step 5. Now open your strings.xml inside values folder and add your unit id here
<!-- This is an ad unit ID for a test ad. Replace with your own banner ad unit id. --> <string name="banner_ad_unit_id">ca-app-pub-3940256099942544/6300978111</string>
Step 6. Now open your MainActivity.java and implement AdMob code
package com.addmob; import android.app.Activity; import android.os.Bundle; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdView; public class MyActivity extends Activity { /** * The view to show the ad. */ private AdView mAdView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_my); mAdView = (AdView)findViewById(R.id.adView); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) //.addTestDevice(AdRequest.Builder.addTestDevice("ABCDEF012345")) .build(); // Start loading the ad in the background. mAdView.loadAd(adRequest); } }
Step 7. Now you need to register your adActivity in your AndroidManifest.xml file to make your add work in application and also need to give Internet permission to load ad from internet.
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.addmob"> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme"> <activity android:name=".MyActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> <meta-data android:name="com.google.android.gms.version" android:value="@integer/google_play_services_version" /> <activity android:theme="@android:style/Theme.Translucent" android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" /> </application> </manifest>