Monday, March 3, 2014

Example code to hide and show the Google Play Services Smart Banner AdView depending on the data connection

A smart banner ad is displayed at the bottom of the app
The Smart Banner ad unit type in the legacy AdMob SDK (6+) can render screen-wide banner ads on any screen size in any orientation. Using it is easy - simply declare the AdView component as a smart banner type with appropriate attributes in an Android activity layout XML file and it will be ready to serve ads - all without having to write a single line of Java code. The Smart Banner will automatically hide or show itself, depending whether the data connection is disabled or enabled when the app is opened; if there is no data connection, then the ad will be hidden. If the data connection is closed after the app has been opened, keep the ad displayed.

However, the old AdMob 6+ SDK will be replaced with the new ads SDK in the Google Play Services. Java code is required to use the Smart Banner AdView. I wanted  the AdView in the new Google Play Services SDK to behave as in the legacy AdMob SDK i.e. hide itself when there is no data connection, and show itself when there is. The following example code snippets show how to do it.

Declare the Smart Banner AdView in the activity layout XML file

<LinearLayout xmlns:android=""
tools:context=".MainActivity" >
Add your layout views here. The total layout weight should be 1...

android:layout_weight="1.0" />

Code an Ad Listener class to hide and show the AdView
This listener will hide the AdView when it is constructed. When an ad has been loaded, it will display the AdView.

package com.dom925.cadmon.seattle;
import android.content.Context;
import android.view.View;
import android.widget.Toast;
public class GoogleAdListener extends AdListener {
private Context _context;
private AdView _adView;
public GoogleAdListener(Context context, AdView adView) {
this._context = context;
_adView = adView; 
//Hide the AdView on creation

public void onAdLoaded() {
//Display the AdView if an Ad is loaded

Code the Activity to use the AdView

public class MainActivity extends FragmentActivity
//Declare a variable for my AdView
private AdView _adView = null;

protected void onCreate(Bundle savedInstanceState) {
//Initialize my AdView and assign the Ad listener to the view
_adView = (AdView)findViewById(;
_adView.setAdListener(new GoogleAdListener(this, _adView));

//Make a request for an ad
protected void onResume() {
//Show the AdView if the data connection is available
if (checkDataConnection()==true){
protected void onPause() {
protected void onDestroy() {
private void requestGoogleAd(AdView adView){
AdRequest adRequest = new AdRequest.Builder()
private boolean checkDataConnection(){
boolean status = false;
ConnectivityManager connectivityMgr = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityMgr.getActiveNetworkInfo()!=null && 
connectivityMgr.getActiveNetworkInfo().isAvailable() &&
connectivityMgr.getActiveNetworkInfo().isConnected()) {
status = true;
return status;

The AdView is not displayed when the app is started without a data connection. 

When the data connection is enabled, the AdView is shown when the app is resumed


Robert Richardson said...


I'm trying to incorporate your code into my app. I created an new xml (google code in separate layout xml). From your instructions, I created a separate activity and fragment as well. Now I'm having trouble wrapping my head around wiring it up so that the add would be placed at the bottom of the screen. Also, I'm not sure as to which value to use for testing onto a specific emulator. I would appreciate your help. I'm still relatively new to android programming.



Robert Richardson said...


Per your instructions, I created a separate XML in the layout folder, and added a fragment and activity class to my existing app that I have on google play. The problem I'm having is wiring it all up so that the add will display on the bottom of the screen. Also, I don't know which number is required for testing on a specific emulator. I hope you can help. Thanks, Bob

dominoc said...

Hi Robert
I have updated the xml layout example. To get the ad view to appear at the bottom, you need to put your layout views above the ad view. The total layout weight of your views should be one; and the height 0, so Android will auto adjust by weight. And you don't need to find a number for the emulator. Just use the pre-defined constant as shown in the example.

Robert Richardson said...

Hi Dominoc,

Thanks for getting back to me. I'm trying to configure this in my app that I have on Google. It's a free app. I see in your code 1 Activity, 1 Fragment, and 1 XML. How can I best wire all this up in my program so that the adds will display in all my screens? As I mentioned, I'm new to android development and with the latest requirements with Google, your blog is the best I've seen on this relatively new subject. Thanks Bob

Turismo Associati said...

What is the coding to do the same thing on a Windows Phone app?

Related Posts with Thumbnails