Google Admob is very popular and best ad network for advertising in mobile applications. With the help of google AdMob, developers can generate revenue from advertisement for their applications. Google AdMob gives many different types of advertisement options like smart banner advertises, full-screen interstitial advertises, video advertises etc. so let’s integrating google AdMob in android app
Create Banner and Interstitial Advertise Unit Ids for integrating google AdMob in your android app
- Signup/Login to Google Admob with your Gmail account.
- Click on Monetise tab.
- click on “+ Monetise new app” button
- Add your app name and select platform
- Generate advertise unit ids for banner and interstitial ads.
Your AndroidMainifest.xml file will add required permissions and Adview activity declaration for integrating google AdMob example
add following dependency in application’s build.gradle file
color.xml in values folder for integrating google AdMob in android app will look like
style.xml in values folder for integrating google AdMob in android app will
strings.xml in values folder for integrating google AdMob in android app will look like bellow, it contains google AdMob banner and interstitial advertise unit ids.
activity_main.xml contains declaration of banner advertisement in XML file for integrating google AdMob tutorial in your android app
MainActivity.java file contains the main logic for integrating google AdMob example, here interstitial advertise will display only after it successfully loaded from server.
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<manifest xmlns:android=
"http://schemas.android.com/apk/res/android"
package=
"com.example.admob"
>
<uses-permission android:name=
"android.permission.INTERNET"
/>
<uses-permission android:name=
"android.permission.ACCESS_NETWORK_STATE"
/>
<application
android:allowBackup=
"true"
android:icon=
"@mipmap/ic_launcher"
android:label=
"@string/app_name"
android:theme=
"@style/AppTheme"
>
<meta-data
android:name=
"com.google.android.gms.version"
android:value=
"@integer/google_play_services_version"
/>
<activity
android:name=
"com.google.android.gms.ads.AdActivity"
android:configChanges=
"keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme=
"@android:style/Theme.Translucent"
/>
<activity
android:name=
".MainActivity"
android:label=
"@string/app_name"
>
<intent-filter>
<action android:name=
"android.intent.action.MAIN"
/>
<category android:name=
"android.intent.category.LAUNCHER"
/>
</intent-filter>
</activity>
</application>
</manifest>
add following dependency in application’s build.gradle file
apply plugin:
'com.android.application'
android {
compileSdkVersion 22
buildToolsVersion
"22.0.1"
defaultConfig {
applicationId
"com.example.admob"
minSdkVersion 19
targetSdkVersion 25
versionCode 1
versionName
"1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(
'proguard-android.txt'
),
'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir:
'libs'
,
include
: [
'*.jar'
])
compile
'com.android.support:appcompat-v7:22.2.1'
compile
'com.google.android.gms:play-services-ads:7.8.0'
}
color.xml in values folder for integrating google AdMob in android app will look like
<?xml version=
"1.0"
encoding=
"utf-8"
?>
<resources>
<color name=
"primaryColor"
>#2286F3</color>
<color name=
"primaryColorDark"
>#2276D2</color>
<color name=
"colorAccent"
>#CC4081</color>
<color name=
"colorTextPrimary"
>#414141</color>
<color name=
"colorTextSecondary"
>#282828</color>
</resources>
<?xml version= "1.0" encoding= "utf-8" ?> <resources> <style name= "AppTheme" parent= "AppTheme.Base" > </style> <style name= "AppTheme.Base" parent= "Theme.AppCompat.Light.NoActionBar" > <item name= "colorPrimary" >@color/primaryColor</item> <item name= "colorPrimaryDark" >@color/primaryColorDark</item> <item name= "colorAccent" >@color/colorAccent</item> <item name= "android:textColorPrimary" >@color/colorTextPrimary</item> <item name= "android:textColorSecondary" >@color/colorTextSecondary</item> </style> </resources> |
strings.xml in values folder for integrating google AdMob in android app will look like bellow, it contains google AdMob banner and interstitial advertise unit ids.
<resources> <string name= "app_name" >Google Admob Demo</string> <string name= "hello_world" >Hello world!</string> <string name= "action_settings" >Settings</string> <string name= "title_activity_main" >MainActivity</string> <string name= "banner_ad_unit_id" >your banner advertise id</string> <string name= "full_screen_ad_unit_id" >your interstial advertise id</string> </resources> |
activity_main.xml contains declaration of banner advertisement in XML file for integrating google AdMob tutorial in your android app
<LinearLayout xmlns:android=
"http://schemas.android.com/apk/res/android"
android:layout_width=
"match_parent"
android:layout_height=
"match_parent"
android:orientation=
"vertical"
tools:context=
"com.example.admob.MainActivity"
>
<
include
layout=
"@layout/app_bar"
/>
<com.google.android.gms.ads.AdView
android:id=
"@+id/ad_view"
android:layout_width=
"match_parent"
android:layout_height=
"wrap_content"
ads:adSize=
"SMART_BANNER"
ads:adUnitId=
"@string/banner_ad_unit_id"
/>
</LinearLayout>
package com.example.admob;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
public
class
MainActivity
extends
AppCompatActivity {
private
Toolbar toolbar;
private
InterstitialAd mInterstitialAd;
private
AdView mAdView;
@Override
protected
void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupBannerAd();
setupInterstialAd();
}
private
void setupBannerAd() {
mAdView = (AdView) findViewById(R.id.ad_view);
AdRequest adRequest =
new
AdRequest.Builder().build();
// Start loading the ad in the background.
mAdView.loadAd(adRequest);
}
private
void setupInterstialAd() {
mInterstitialAd =
new
InterstitialAd(MainActivity.this);
mInterstitialAd.setAdUnitId(getResources().getString(R.string.full_screen_ad_unit_id));
AdRequest adRequestFull =
new
AdRequest.Builder().build();
mInterstitialAd.loadAd(adRequestFull);
mInterstitialAd.setAdListener(
new
AdListener() {
@Override
public
void onAdLoaded() {
super.onAdLoaded();
// Full screen advertise will show only after loading complete
mInterstitialAd.show();
}
});
}
}
No comments:
Post a Comment