Java Code Examples for com.google.android.gms.ads.AdView#setAdListener()

The following examples show how to use com.google.android.gms.ads.AdView#setAdListener() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GooglePlayServicesBanner.java    From mobile-sdk-android with Apache License 2.0 6 votes vote down vote up
/**
 * Interface called by the AN SDK to request an ad from the mediating SDK.
 *
 * @param mBC                 the object which will be called with events from the 3rd party SDK
 * @param activity            the activity from which this is launched
 * @param parameter           String parameter received from the server for instantiation of this object
 * @param adUnitID            The 3rd party placement , in adMob this is the adUnitID
 * @param width               Width of ad
 * @param height              Height of ad
 * @param targetingParameters targetingParameters
 */
@Override
public View requestAd(MediatedBannerAdViewController mBC, Activity activity, String parameter,
                      String adUnitID, int width, int height, TargetingParameters targetingParameters) {
    adListener = new GooglePlayAdListener(mBC, super.getClass().getSimpleName());
    adListener.printToClog(String.format(" - requesting an ad: [%s, %s, %dx%d]",
            parameter, adUnitID, width, height));

    adView = new AdView(activity);
    adView.setAdUnitId(adUnitID);
    adView.setAdSize(new AdSize(width, height));
    adView.setAdListener(adListener);

    try {
        adView.loadAd(buildRequest(targetingParameters));
    } catch (NoClassDefFoundError e) {
        // This can be thrown by Play Services on Honeycomb.
        adListener.onAdFailedToLoad(AdRequest.ERROR_CODE_NO_FILL);
    }

    return adView;
}
 
Example 2
Source File: AdMobComboProvider.java    From remixed-dungeon with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void displayBanner() {

    adView = new AdView(Game.instance());
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(Game.getVar(R.string.easyModeAdUnitId));
    adView.setBackgroundColor(Color.TRANSPARENT);
    adView.setAdListener(new AdmobBannerListener());

    adView.loadAd(AdMob.makeAdRequest());
}
 
Example 3
Source File: MainActivity.java    From android-ads with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the banner ads in the items list.
 */
private void loadBannerAd(final int index) {

    if (index >= recyclerViewItems.size()) {
        return;
    }

    Object item = recyclerViewItems.get(index);
    if (!(item instanceof AdView)) {
        throw new ClassCastException("Expected item at index " + index + " to be a banner ad"
                + " ad.");
    }

    final AdView adView = (AdView) item;

    // Set an AdListener on the AdView to wait for the previous banner ad
    // to finish loading before loading the next ad in the items list.
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            // The previous banner ad loaded successfully, call this method again to
            // load the next ad in the items list.
            loadBannerAd(index + ITEMS_PER_AD);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // The previous banner ad failed to load. Call this method again to load
            // the next ad in the items list.
            Log.e("MainActivity", "The previous banner ad failed to load. Attempting to"
                    + " load the next banner ad in the items list.");
            loadBannerAd(index + ITEMS_PER_AD);
        }
    });

    // Load the banner ad.
    adView.loadAd(new AdRequest.Builder().build());
}
 
Example 4
Source File: AdLayout.java    From KernelAdiutor with GNU General Public License v3.0 5 votes vote down vote up
public AdLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    setOrientation(VERTICAL);

    LayoutInflater.from(context).inflate(R.layout.ad_layout_view, this);
    FrameLayout mAdLayout = findViewById(R.id.ad_layout);
    mProgress = findViewById(R.id.progress);
    mAdText = findViewById(R.id.ad_text);
    mGHImage = findViewById(R.id.gh_image);

    findViewById(R.id.remove_ad).setOnClickListener(v
            -> ViewUtils.dialogDonate(v.getContext()).show());

    mAdView = new AdView(context);
    mAdView.setAdSize(AdSize.SMART_BANNER);
    mAdView.setAdUnitId("ca-app-pub-1851546461606210/7537613480");
    mAdView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            mAdFailedLoading = false;
            mProgress.setVisibility(GONE);
            if (mAdView.getParent() == null) {
                mAdLayout.addView(mAdView);
            }
        }

        @Override
        public void onAdFailedToLoad(int i) {
            super.onAdFailedToLoad(i);
            mAdFailedLoading = true;
            loadGHAd();
        }
    });
    mAdView.loadAd(new AdRequest.Builder().build());
}
 
Example 5
Source File: AdMobManager.java    From ANEAdMob with Apache License 2.0 5 votes vote down vote up
public void show(String adID, int size, /*int autoHW,*/ int halign, int valign, String testDevice)
{
	hide();
	
	switch(size)
	{
	case 1: _adSize = AdSize.BANNER; break; //set by default, but leave it here for reference
	case 2: _adSize = AdSize.MEDIUM_RECTANGLE; break;
	case 3: _adSize = AdSize.FULL_BANNER; break;
	case 4: _adSize = AdSize.LEADERBOARD; break;
	case 5: _adSize = AdSize.SMART_BANNER; break;
	case 6: _adSize = AdSize.WIDE_SKYSCRAPER; break;
	}
	
	_adView = new AdView(_act);
	_adView.setAdUnitId(adID);
	_adView.setAdSize(_adSize);
	
	AdRequest adRequest = null;
	if(testDevice == null) //no test device
		adRequest = new AdRequest.Builder().build();
	else
		adRequest = new AdRequest.Builder().addTestDevice(testDevice).build(); //eto pizdec
	
	_adView.setAdListener(new AdMobListener(_ctx, "BANNER"));
	
	_params = new RelativeLayout.LayoutParams(-2, -2);
	_params.addRule(halign, -1);
	_params.addRule(valign, -1);
	
	_parentView.addView(_adView, _params);

_adView.loadAd(adRequest);
}
 
Example 6
Source File: MainActivity.java    From googleads-mobile-android-examples with Apache License 2.0 5 votes vote down vote up
/**
 * Loads the banner ads in the items list.
 */
private void loadBannerAd(final int index) {

    if (index >= recyclerViewItems.size()) {
        return;
    }

    Object item = recyclerViewItems.get(index);
    if (!(item instanceof AdView)) {
        throw new ClassCastException("Expected item at index " + index + " to be a banner ad"
                + " ad.");
    }

    final AdView adView = (AdView) item;

    // Set an AdListener on the AdView to wait for the previous banner ad
    // to finish loading before loading the next ad in the items list.
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            // The previous banner ad loaded successfully, call this method again to
            // load the next ad in the items list.
            loadBannerAd(index + ITEMS_PER_AD);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            // The previous banner ad failed to load. Call this method again to load
            // the next ad in the items list.
            Log.e("MainActivity", "The previous banner ad failed to load. Attempting to"
                    + " load the next banner ad in the items list.");
            loadBannerAd(index + ITEMS_PER_AD);
        }
    });

    // Load the banner ad.
    adView.loadAd(new AdRequest.Builder().build());
}
 
Example 7
Source File: DownloadProfileImageActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 8
Source File: MainPageActivity.java    From AndroidFaceRecognizer with MIT License 4 votes vote down vote up
@Override
protected void onCreate(Bundle savedInstanceState) {
	super.onCreate(savedInstanceState);
	setContentView(R.layout.main_page);
	
	
	adView = new AdView(this);
	adView.setAdUnitId("ca-app-pub-4147454460675251/3837655321");
	adView.setAdSize(AdSize.BANNER);
	
	adView.setAdListener(adListener);
	
	RelativeLayout adLayout = (RelativeLayout)findViewById(R.id.main_page_adview_layout);
	adLayout.addView(adView);
	
	RelativeLayout.LayoutParams adViewlp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
	adViewlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
	adViewlp.addRule(RelativeLayout.CENTER_HORIZONTAL);
	adView.setLayoutParams(adViewlp);

	
	AdRequest request = new AdRequest.Builder()
	.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
    .addTestDevice("04E14595EE84A505616E50A99B15252D")
    .build();
	
	
	adView.loadAd(request);
	
	System.out.println("---------------------- istestdevice: "+request.isTestDevice(this));

	
	mContext = this;
	
	DisplayMetrics dm = new DisplayMetrics();
	getWindowManager().getDefaultDisplay().getMetrics( dm );
	
	screenWidth = dm.widthPixels;
	screenHeight = dm.heightPixels;
	
	final TextView headerText = (TextView)findViewById(R.id.mainHeaderText);
	RelativeLayout.LayoutParams headerTextParams = (RelativeLayout.LayoutParams)headerText.getLayoutParams();
	headerTextParams.leftMargin = screenHeight/8;
	headerText.setLayoutParams(headerTextParams);
	headerText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, (float)screenHeight/35);
	headerText.setText("Face Recognition");
	headerText.setTextColor(Color.LTGRAY);
	headerText.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
	headerText.setTypeface(null, Typeface.BOLD);
	
	ImageView headerIcon = (ImageView)findViewById(R.id.mainHeaderIcon);
	RelativeLayout.LayoutParams iconLParams = new RelativeLayout.LayoutParams(screenHeight/11, screenHeight/11);
	iconLParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT | RelativeLayout.CENTER_VERTICAL);
	iconLParams.leftMargin = screenHeight/80;
	headerIcon.setLayoutParams(iconLParams);
	
	
	ListView listView = (ListView)findViewById(R.id.mainListView);
	ListAdapter listAdapter = new ListAdapter();
	listView.setAdapter(listAdapter);
	listView.setOnItemClickListener(itemClickListener);
	
	RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)listView.getLayoutParams();
	params.leftMargin = screenWidth/10;
	params.rightMargin = screenWidth/10;
	params.topMargin = screenHeight/12;
	listView.setLayoutParams(params);
	listView.setVerticalScrollBarEnabled(false);

}
 
Example 9
Source File: TestAdvancedAdmobActivity.java    From UltimateRecyclerView with Apache License 2.0 4 votes vote down vote up
private RelativeLayout createadmob() {

        AdSize adSize = AdSize.SMART_BANNER;

        DisplayMetrics dm = getResources().getDisplayMetrics();

        double density = dm.density * 160;
        double x = Math.pow(dm.widthPixels / density, 2);
        double y = Math.pow(dm.heightPixels / density, 2);
        double screenInches = Math.sqrt(x + y);

        if (screenInches > 8) { // > 728 X 90
            adSize = AdSize.LEADERBOARD;
        } else if (screenInches > 6) { // > 468 X 60
            adSize = AdSize.MEDIUM_RECTANGLE;
        } else { // > 320 X 50
            adSize = AdSize.BANNER;
        }

        adSize = AdSize.MEDIUM_RECTANGLE;
        final AdView mAdView = new AdView(this);
        mAdView.setAdSize(adSize);
        mAdView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");
        // Create an ad request.
        AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
        if (admob_test_mode)
            // Optionally populate the ad request builder.
            adRequestBuilder.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
        // Start loading the ad.
        mAdView.loadAd(adRequestBuilder.build());
        DisplayMetrics displaymetrics = new DisplayMetrics();
        final RelativeLayout layout = AdGoogleDisplaySupport.initialSupport(this, displaymetrics);
        final double ratio = AdGoogleDisplaySupport.ratioMatching(displaymetrics);
        final int ad_height = AdGoogleDisplaySupport.defaultHeight(displaymetrics);
        AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (ad_height * ratio));
        // get display info
        /*  G.display_w = displayMetrics.widthPixels;
        G.display_h = displayMetrics.heightPixels;
        G.scale = Math.max(G.display_w/1280.0f, G.display_h/800.0f);*/
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                super.onAdLoaded();
                int h = mAdView.getLayoutParams().height;
                AdGoogleDisplaySupport.scale(mAdView, ratio);
                AdGoogleDisplaySupport.panelAdjust(mAdView, (int) (h * ratio));
                //  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP)
            }
        });
        layout.addView(mAdView);
        return layout;
    }
 
Example 10
Source File: MainActivity.java    From HeartbeatFixerForGCM with Apache License 2.0 4 votes vote down vote up
private void showAd() {
    Log.v(TAG, "showAd");
    if (!mShowAd) {
        mShowAd = true;
        mAdView = new AdView(this);
        mAdView.setAdSize(AdSize.SMART_BANNER);
        mAdView.setAdUnitId("ca-app-pub-5964196502067291/5460955376");
        mAdView.setAdListener(new AdListener() {
            @Override
            public void onAdClosed() {
                Log.v(TAG, "onAdClosed");
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                Log.v(TAG, "onAdFailedToLoad, errorCode: " + errorCode);
            }

            @Override
            public void onAdLeftApplication() {
                Log.v(TAG, "onAdLeftApplication");
            }

            @Override
            public void onAdOpened() {
                Log.v(TAG, "onAdOpened");
            }

            @Override
            public void onAdLoaded() {
                Log.v(TAG, "onAdLoaded");
            }

            @Override
            public void onAdClicked() {
                Log.v(TAG, "onAdClicked");
            }

            @Override
            public void onAdImpression() {
                Log.v(TAG, "onAdImpression");
            }
        });
        getRootViewGroup().addView(mAdView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        mAdView.loadAd(new AdRequest.Builder().build());
    }
}
 
Example 11
Source File: AdWrapper.java    From FireFiles with Apache License 2.0 4 votes vote down vote up
public void initAd(){
    mAdView = (AdView) findViewById(R.id.adView);
    mAdView.setAdListener(adListener);
}
 
Example 12
Source File: SearchActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 13
Source File: StoriesFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 14
Source File: ViewStoryActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.SMART_BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 15
Source File: ViewProfileActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 16
Source File: DownloadHistoryActivity.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {

                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 17
Source File: DownloadIGTVFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {

        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 18
Source File: ProfileFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    }catch (Exception e){
        Log.e(TAG, "showBannerAd: "+e );
    }
}
 
Example 19
Source File: StoriesOverview.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}
 
Example 20
Source File: DownloadPostFragment.java    From Instagram-Profile-Downloader with MIT License 4 votes vote down vote up
private void showBannerAd() {
    try {
        adView = new AdView(getActivity());
        adView.setAdSize(AdSize.MEDIUM_RECTANGLE);
        adView.setAdUnitId(getString(R.string.banner_home_footer));
        adContainer.addView(adView);
        AdRequest adRequest = new AdRequest.Builder()
                .build();
        adView.loadAd(adRequest);
        adView.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                adContainer.setVisibility(View.VISIBLE);
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
                showFbBannerAd();
            }

            @Override
            public void onAdOpened() {
                // Code to be executed when an ad opens an overlay that
                // covers the screen.
            }

            @Override
            public void onAdLeftApplication() {
                // Code to be executed when the user has left the app.
            }

            @Override
            public void onAdClosed() {
                // Code to be executed when the user is about to return
                // to the app after tapping on an ad.
            }
        });
    } catch (Exception e) {
        Log.e(TAG, "showBannerAd: " + e);
    }
}