Você está na página 1de 10

11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

 Home  /  AcadGild • Android • Digital Marketing  /  Google Admob YES, I WANT TO BOOST MY


with Banner and Interstitial Ads in Android Apps CAREER & INCREASE MY
SALARY!

Your Name (required)

Your Email (required)

Your Contact Number


(required)

05 Google Admob
JULY
Your Message

with Banner and


2017

Interstitial Ads TELL ME HOW

in Android Apps
MASTERING BIG DATA
WITH REAL WORLD
PROJECTS
VIDEO TUTORIALS
REAL-WORLD HADOOP
Error USE
type:CASES
"Forbidden".
E-BOOKError
This Blog is in continuation with our previous blog “Beginner’s Guide
message: "The request cannot
to AdMob – a Portmanteau for “Advertising on Mobile” where we be completed because you
talked all about AdMob and how to monetize your App with Ads. have exceeded your quota."
Here we will go a step forward and talk about how to actually write Domain: "youtube.quota".
Reason: "quotaExceeded".
code for the App to create AdMob in it.

Did you added your own


Below we will be discussing the Google API key? Look at the
code: AdMob help.

Create a Sample Application for Two Types of AdMob about Banner Ad Check in YouTube if the id

and Interstitial Ad. First we will talk about Banner Ad. UCaQfgvMsjpImSxrJQDBjd-Q
belongs to a channelid. Check
the FAQ of the plugin or send
Banner Ad App: error messages to support.

ACADGILD
1. First of all add the code for Google Ad services in build.gradle
DOWNLOADS & EBOOKS
le In
Learn. Do. dependencies:
Earn.

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 1/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

1 compile 'com.google.android.gms:play-services:7.0.0' LIKE WHAT YOU SEE?


SUBSCRIBE TO OUR BLOG
2. In the manifest le i.e AndroidManifest.xml add below code
We send only 1 email in a
week

Fascinated by Enter your email...

latest technology? Subscribe

Subscribe to our blog and we'll keep you


updated with latest technologies like
AngularJS, Machine Learning, Big Data,
Spark, Android, Analytics, NodeJS,
Cloud Computing and many more!

Name Email Subscribe,


It’s Free!

1 <?xml version="1.0" encoding="utf-8"?>


2 <manifest xmlns:android="http://schemas.android.com/apk/res/a
3 ndroid"
4 package="com.google.android.gms.example.bannerexample"
5 >
6 <!-- Include required permissions for Google Mobile Ads to
7 run-->
8 <uses-permission android:name="android.permission.INTERNE
9 T"/>
10 <uses-permission android:name="android.permission.ACCESS_
11 NETWORK_STATE"/>
12 SEARCH
13 <application
14 android:allowBackup="true"
15 android:icon="@drawable/ic_launcher"  Search Now
16 android:label="@string/app_name"
17 android:theme="@style/AppTheme" >
18 <!--This meta-data tag is required to use Google Play Servi
19 ces.-->
20 <meta-data android:name="com.google.android.gms.versio
21 n"/> CATEGORIES
22 <activity
23 android:name=".MyActivity" AcadGild
24 android:label="@string/app_name" >
25 <intent-filter>
26 <action android:name="android.intent.action.MAIN" /> An Hour with the
Crackerjacks of the Tech
27 <category android:name="android.intent.category.LAU
28 NCHER" />
World
29 </intent-filter>
30 </activity>
ACADGILD <!--Include the AdActivity configChanges and theme. -->
Android
DOWNLOADS & EBOOKS
<activity android:name="com.google.android.gms.ads.AdAct
Learn. Do. Earn.
ivity" Android For Kids
https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 2/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

android:configChanges="keyboard|keyboardHidden|orient
ation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" /> AngularJS
</application>
Arti cial Intelligence
</manifest>

Big Data and Hadoop


3. Add your banner Ad unit ID to string.xml le:
Big Data and Hadoop
Advanced
1 <!-- This is an ad unit ID for a test ad. Replace with your own ban
2 ner ad unit id. -->
<string name="banner_ad_uncait_id">-app-pub-394025609994 Careers
2544/6300978111</string>
Cloud computing
Note: This banner ID key is for my Ad. Since you have already
learn how to generate banner ID key, create a new Ad ID for Cyber Security
your app.
Database
 
DevOps

4. Add below code to MainActivity.java


Digital Marketing

1 package com.google.android.gms.example.bannerexample;
Ethical Hacking
2
3 import android.os.Bundle; Front End
4 import android.support.v7.app.ActionBarActivity;
5 import android.view.Menu;
6 import android.view.MenuItem; Full Stack
7 import android.webkit.WebView;
8
9 import com.google.android.gms.ads.AdRequest; Hadoop Administration
10 import com.google.android.gms.ads.AdView;
11
Interview Question
12 public class MyActivity extends AppCompatActivity {
13
14 private AdView mAdView; IOS
15 private WebView webview;
16 @Override
17 protected void onCreate(Bundle savedInstanceState) { Java
18 super.onCreate(savedInstanceState);
19 setContentView(R.layout.activity_my);
20 mAdView = (AdView) findViewById(R.id.ad_view); Java
21
22 AdRequest adRequest = new AdRequest.Builder()
KAFKA
23 .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
24 .build();
25 mAdView.loadAd(adRequest); Kids
26
27 webview = (WebView) findViewById(R.id.webView);
28 Linux Administration
29 webview.getSettings().setJavaScriptEnabled(true);
30 webview.loadUrl(" https://acadgild.com/blog/category/androi
31 d/"); Mongo DB
32
33 }
NodeJS
34
35 @Override

ACADGILD
36
37
public boolean onCreateOptionsMenu(Menu menu) { Others
DOWNLOADS & EBOOKS
38 Do. Earn.
Learn. getMenuInflater().inflate(R.menu.my, menu);
39 return true; PMP
https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 3/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

40 }
41
Prince2
42 @Override
43 public boolean onOptionsItemSelected(MenuItem item) {
44 int id = item.getItemId(); Python
45 if (id == R.id.action_settings) {
46 return true;
47 } Quiz
48 return super.onOptionsItemSelected(item);
49 }
50 R & Machine Learning
51 @Override
52 public void onPause() {
Scala
53 if (mAdView != null) {
54 mAdView.pause();
55 } Spark
56 super.onPause();
57 }
58 Techie Fridays
59 @Override
60 public void onResume() {
61 super.onResume(); UI/UX
62 if (mAdView != null) {
63 mAdView.resume();
Uncategorized
64 }
65 }
66 DevOps
67 @Override
68 public void onDestroy() {
69 if (mAdView != null) {
70 mAdView.destroy();
71 }
72 super.onDestroy(); GET SOCIAL
73 }
}

5. Add this code to design your ad in “activity_main.xml” le

1 <RelativeLayout xmlns:android="http://schemas.android.com/ap
2 k/res/android"
3 xmlns:tools="http://schemas.android.com/tools"
4 xmlns:ads="http://schemas.android.com/apk/res-auto"
5 android:id="@+id/container"
6 android:layout_width="match_parent"
7 android:layout_height="match_parent"
8 android:layout_marginTop="@dimen/activity_vertical_margin"
9 tools:context=".MyActivity"
10 tools:ignore="MergeRootFrame">
11
12 <com.google.android.gms.ads.AdView
13 android:id="@+id/ad_view"
14 android:layout_width="match_parent"
15 android:layout_height="wrap_content"
16 android:layout_centerHorizontal="true"
17 android:layout_alignParentBottom="true"
18 ads:adSize="BANNER"
19 ads:adUnitId="@string/banner_ad_unit_id" />
20
21 <WebView
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content"
24 android:id="@+id/webView"
25 android:layout_alignParentTop="true"
ACADGILD
26
27
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
DOWNLOADS & EBOOKS
Learn.
28 Do. Earn.
android:layout_above="@+id/ad_view"

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 4/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

29 android:layout_alignParentRight="true"
30 android:layout_alignParentEnd="true" /> AcadGild
31 156,343 likes
</RelativeLayout>

Run the code and you will see the output as shown in gure below:
Like Page

Be the first of your friends to like this

AcadGild
pada hari Minggu

#AcadgildStories #Developer
#Programmer

gph.is
MEDIA.GIPHY.COM

WHAT’S TRENDING

RECENT POSTS

Introduction to
Expandable
RecyclerView in
Android
 November 7,
2017

With this we have successfully learnt how to create and display Ad at Exploring the new
the bottom of the page. Now we will see how to create an Interstitial Constraint Layout
Ad in the App. in Android
 November 2,
2017
Interstitial Ad App
ACADGILD DOWNLOADS & EBOOKS
Introduction to
Learn. Do. Earn.
Fragments in
https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 5/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

1. First of all add the code for Google Ad Android


 October 31,
services in build.gradle le In 2017
dependencies
Java vs Android:
Which Might Be a
1 compile 'com.google.android.gms:play-services:7.0.0' Better Career
Options?
2. In the manifest le i.e AndroidManifest.xml add below code  October 30,
2017

1 <?xml version="1.0" encoding="utf-8"?>


2 <manifest xmlns:android="http://schemas.android.com/apk/res/a
3 ndroid"
4 package="com.google.android.gms.example.interstitialexampl
5 e" > ARCHIVES
6 <!-- Include required permissions for Google Mobile Ads to run.
7 --> November 2017
8 <uses-permission android:name="android.permission.INTERNE
9 T"/>
10 <uses-permission android:name="android.permission.ACCESS_ October 2017
11 NETWORK_STATE"/>
12
September 2017
13 <application
14 android:allowBackup="true"
android:icon="@drawable/ic_launcher" August 2017
15 android:label="@string/app_name"
16 android:theme="@style/AppTheme" >
17 <!--This meta-data tag is required to use Google Play Servi July 2017
18 ces. -->
19 <meta-data android:name="com.google.android.gms.versio
20 n" android:value="@integer/google_play_services_version"/> June 2017
21 <activity
22 android:name=".MyActivity"
May 2017
23 android:label="@string/app_name" >
24 <intent-filter>
25 <action android:name="android.intent.action.MAIN" /> April 2017
26
<category android:name="android.intent.category.LAU
27 NCHER" /> March 2017
28 </intent-filter>
29 </activity>
30 <!--Include the AdActivity configChanges and theme. --> February 2017
<activity android:name="com.google.android.gms.ads.AdAct
ivity"
January 2017
android:configChanges="keyboard|keyboardHidden|orient
ation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:theme="@android:style/Theme.Translucent" /> 
December 2016
</application>

</manifest> November 2016

October 2016
3. Add this code to design your ad in “activity_main.xml” le

September 2016

1 <RelativeLayout xmlns:android="http://schemas.android.com/ap
2 k/res/android" August 2016
3 xmlns:tools="http://schemas.android.com/tools"
4 android:id="@+id/container"
5 android:layout_width="match_parent" July 2016
ACADGILD
6
7
android:layout_height="match_parent"
tools:context=".MyActivity"
DOWNLOADS & EBOOKS
Learn. June 2016
8 Do. Earn.
tools:ignore="MergeRootFrame">

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 6/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

9 May 2016
10 <TextView
11 android:id="@+id/game_title"
12 android:layout_width="wrap_content" April 2016
13 android:layout_height="wrap_content"
14 android:layout_centerHorizontal="true"
15 android:layout_marginTop="50dp" March 2016
16 android:text="@string/impossible_game"
17 android:textAppearance="?android:attr/textAppearanceLarg
February 2016
18 e" />
19
20 <TextView January 2016
21 android:id="@+id/timer"
22 android:layout_width="wrap_content"
23 android:layout_height="wrap_content" December 2015
24 android:layout_below="@+id/game_title"
25 android:layout_centerHorizontal="true"
26 android:textAppearance="?android:attr/textAppearanceLarg
November 2015
27 e" />
28 August 2015
29 <Button
30 android:id="@+id/retry_button"
31 android:layout_width="wrap_content" June 2015
32 android:layout_height="wrap_content"
33 android:layout_centerHorizontal="true"
34 android:layout_centerVertical="true" May 2015
android:text="Retry" />
November 2014
</RelativeLayout>

4. Add below code to MainActivity.java October 2014

September 2014
1 package com.google.android.gms.example.interstitialexample;
2 August 2014
3 import android.os.Bundle;
4 import android.os.CountDownTimer;
5 import android.support.v7.app.ActionBarActivity;
6 import android.view.View;
7 import android.widget.Button;
8 import android.widget.TextView;
9 import android.widget.Toast;
10
11 import com.google.android.gms.ads.AdListener;
12 import com.google.android.gms.ads.AdRequest;
13 import com.google.android.gms.ads.InterstitialAd;
14
15 public class MyActivity extends ActionBarActivity {
16
17 private static final long GAME_LENGTH_MILLISECONDS = 3
18 000;
19
20 private InterstitialAd mInterstitialAd;
21 private CountDownTimer mCountDownTimer;
22 private Button mRetryButton;
23 private boolean mGameIsInProgress;
24 private long mTimerMilliseconds;
25
26 @Override
27 protected void onCreate(Bundle savedInstanceState) {
28 super.onCreate(savedInstanceState);
29 setContentView(R.layout.activity_my);
30
31 // Create the InterstitialAd and set the adUnitId.
32 mInterstitialAd = new InterstitialAd(this);
ACADGILD
33 DOWNLOADS & EBOOKS
34 Do. Earn.// Defined in res/values/strings.xml
Learn.
35 mInterstitialAd.setAdUnitId(getString(R.string.ad_unit_id));
https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 7/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

36
37 mInterstitialAd.setAdListener(new AdListener() {
38 @Override
39 public void onAdClosed() {
40 startGame();
41 }
42 });
43
44
45 mRetryButton = ((Button)
46 findViewById(R.id.retry_button));
47 mRetryButton.setVisibility(View.INVISIBLE);
48 mRetryButton.setOnClickListener(new
49 View.OnClickListener() {
50 @Override
51 public void onClick(View view) {
52 showInterstitial();
53 }
54 });
55
56 startGame();
57 }
58
59 private void createTimer(final long milliseconds) {
60
61 if (mCountDownTimer != null) {
62 mCountDownTimer.cancel();
63 }
64
65 final TextView textView = ((TextView) findViewById(R.id.ti
66 mer));
67
68 mCountDownTimer = new CountDownTimer(milliseconds, 5
69 0) {
70 @Override
71 public void onTick(long millisUnitFinished) {
72 mTimerMilliseconds = millisUnitFinished;
73 textView.setText("seconds remaining: " + ((millisUnitF
74 inished / 1000) + 1));
75 }
76
77 @Override
78 public void onFinish() {
79 mGameIsInProgress = false;
80 textView.setText("done!");
81 mRetryButton.setVisibility(View.VISIBLE);
82 }
83 };
84 }
85
86 @Override
87 public void onResume() {
88 // Start or resume the game.
89 super.onResume();
90
91 if (mGameIsInProgress) {
92 resumeGame(mTimerMilliseconds);
93 }
94 }
95
96 @Override
97 public void onPause() {
98 // Cancel the timer if the game is paused
99 mCountDownTimer.cancel();
100 super.onPause();
101 }
ACADGILD
102
103 private void showInterstitial() {
DOWNLOADS & EBOOKS
Learn.
104Do. Earn.

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 8/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

105 // Show the ad if it's ready. Otherwise toast and restart the
106 game.
107 if (mInterstitialAd != null && mInterstitialAd.isLoaded()) {
108 mInterstitialAd.show();
109 } else {
110 Toast.makeText(this, "Ad did not load", Toast.LENGTH_S
111 HORT).show();
112 startGame();
113 }
114 }
115
116 private void startGame() {
117
118 if (!mInterstitialAd.isLoaded() &&
119 !mInterstitialAd.isLoaded()) {
120 AdRequest adRequest = new
121 AdRequest.Builder().build();
122 mInterstitialAd.loadAd(adRequest);
123 }
124
mRetryButton.setVisibility(View.INVISIBLE);
resumeGame(GAME_LENGTH_MILLISECONDS);
}

private void resumeGame(long milliseconds) {


mGameIsInProgress = true;
mTimerMilliseconds = milliseconds;
createTimer(milliseconds);
mCountDownTimer.start();
}
}

Run this code and you will get an output as shown in gure below:

ACADGILD DOWNLOADS & EBOOKS


Learn. Do. Earn.

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 9/14
11/7/2017 Google Admob with Banner and Interstitial Ads in Android Apps |

Hope with these two blogs you now have a clear idea on how to
create Ad in your App using AdMob.

Keep visiting our website Acadgild for more updates on Big Data and
other technologies. Click here to learn Android App Development.

ACADGILD DOWNLOADS & EBOOKS


Learn. Do. Earn.

https://acadgild.com/blog/google-admob-with-banner-and-interstitial-ads-in-android-apps/ 10/14

Você também pode gostar