Download File
Download Project
Settings
Line Wrap
Themes
default
ambiance
bespin
dracula
eclipse
material
mbo
mdn-like
neat
solarized dark
ttcn
zenburn
MainActivity.java
/* * Copyright (C) 2015 Google, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package com.google.example.gms.nativeadvancedexample; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.CheckBox; import android.widget.FrameLayout; import android.widget.ImageView; import android.widget.RatingBar; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdLoader; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.MobileAds; import com.google.android.gms.ads.VideoController; import com.google.android.gms.ads.VideoOptions; import com.google.android.gms.ads.formats.MediaView; import com.google.android.gms.ads.formats.NativeAd; import com.google.android.gms.ads.formats.NativeAdOptions; import com.google.android.gms.ads.formats.NativeAppInstallAd; import com.google.android.gms.ads.formats.NativeAppInstallAdView; import com.google.android.gms.ads.formats.NativeContentAd; import com.google.android.gms.ads.formats.NativeContentAdView; import java.util.List; import java.util.Locale; /** * A simple activity class that displays native ad formats. */ public class MainActivity extends AppCompatActivity { private static final String ADMOB_AD_UNIT_ID = "ca-app-pub-3940256099942544/2247696110"; private static final String ADMOB_APP_ID = "ca-app-pub-3940256099942544~3347511713"; private Button mRefresh; private CheckBox mRequestAppInstallAds; private CheckBox mRequestContentAds; private CheckBox mStartVideoAdsMuted; private TextView mVideoStatus; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); // Initialize the Mobile Ads SDK. MobileAds.initialize(this, ADMOB_APP_ID); mRefresh = findViewById(R.id.btn_refresh); mRequestAppInstallAds = findViewById(R.id.cb_appinstall); mRequestContentAds = findViewById(R.id.cb_content); mStartVideoAdsMuted = findViewById(R.id.cb_start_muted); mVideoStatus = findViewById(R.id.tv_video_status); mRefresh.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { refreshAd(mRequestAppInstallAds.isChecked(), mRequestContentAds.isChecked()); } }); refreshAd(mRequestAppInstallAds.isChecked(), mRequestContentAds.isChecked()); } /** * Populates a {@link NativeAppInstallAdView} object with data from a given * {@link NativeAppInstallAd}. * * @param nativeAppInstallAd the object containing the ad's assets * @param adView the view to be populated */ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd, NativeAppInstallAdView adView) { // Get the video controller for the ad. One will always be provided, even if the ad doesn't // have a video asset. VideoController vc = nativeAppInstallAd.getVideoController(); // Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The // VideoController will call methods on this object when events occur in the video // lifecycle. vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() { public void onVideoEnd() { // Publishers should allow native ads to complete video playback before refreshing // or replacing them with another ad in the same UI location. mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Video playback has ended."); super.onVideoEnd(); } }); adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline)); adView.setBodyView(adView.findViewById(R.id.appinstall_body)); adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action)); adView.setIconView(adView.findViewById(R.id.appinstall_app_icon)); adView.setPriceView(adView.findViewById(R.id.appinstall_price)); adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars)); adView.setStoreView(adView.findViewById(R.id.appinstall_store)); // Some assets are guaranteed to be in every NativeAppInstallAd. ((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody()); ((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction()); ((ImageView) adView.getIconView()).setImageDrawable( nativeAppInstallAd.getIcon().getDrawable()); MediaView mediaView = adView.findViewById(R.id.appinstall_media); ImageView mainImageView = adView.findViewById(R.id.appinstall_image); // Apps can check the VideoController's hasVideoContent property to determine if the // NativeAppInstallAd has a video asset. if (vc.hasVideoContent()) { adView.setMediaView(mediaView); mainImageView.setVisibility(View.GONE); mVideoStatus.setText(String.format(Locale.getDefault(), "Video status: Ad contains a %.2f:1 video asset.", vc.getAspectRatio())); } else { adView.setImageView(mainImageView); mediaView.setVisibility(View.GONE); // At least one image is guaranteed. List
images = nativeAppInstallAd.getImages(); mainImageView.setImageDrawable(images.get(0).getDrawable()); mRefresh.setEnabled(true); mVideoStatus.setText("Video status: Ad does not contain a video asset."); } // These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to // check before trying to display them. if (nativeAppInstallAd.getPrice() == null) { adView.getPriceView().setVisibility(View.INVISIBLE); } else { adView.getPriceView().setVisibility(View.VISIBLE); ((TextView) adView.getPriceView()).setText(nativeAppInstallAd.getPrice()); } if (nativeAppInstallAd.getStore() == null) { adView.getStoreView().setVisibility(View.INVISIBLE); } else { adView.getStoreView().setVisibility(View.VISIBLE); ((TextView) adView.getStoreView()).setText(nativeAppInstallAd.getStore()); } if (nativeAppInstallAd.getStarRating() == null) { adView.getStarRatingView().setVisibility(View.INVISIBLE); } else { ((RatingBar) adView.getStarRatingView()) .setRating(nativeAppInstallAd.getStarRating().floatValue()); adView.getStarRatingView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeAppInstallAd); } /** * Populates a {@link NativeContentAdView} object with data from a given * {@link NativeContentAd}. * * @param nativeContentAd the object containing the ad's assets * @param adView the view to be populated */ private void populateContentAdView(NativeContentAd nativeContentAd, NativeContentAdView adView) { mVideoStatus.setText("Video status: Ad does not contain a video asset."); mRefresh.setEnabled(true); adView.setHeadlineView(adView.findViewById(R.id.contentad_headline)); adView.setImageView(adView.findViewById(R.id.contentad_image)); adView.setBodyView(adView.findViewById(R.id.contentad_body)); adView.setCallToActionView(adView.findViewById(R.id.contentad_call_to_action)); adView.setLogoView(adView.findViewById(R.id.contentad_logo)); adView.setAdvertiserView(adView.findViewById(R.id.contentad_advertiser)); // Some assets are guaranteed to be in every NativeContentAd. ((TextView) adView.getHeadlineView()).setText(nativeContentAd.getHeadline()); ((TextView) adView.getBodyView()).setText(nativeContentAd.getBody()); ((TextView) adView.getCallToActionView()).setText(nativeContentAd.getCallToAction()); ((TextView) adView.getAdvertiserView()).setText(nativeContentAd.getAdvertiser()); List
images = nativeContentAd.getImages(); if (images.size() > 0) { ((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable()); } // Some aren't guaranteed, however, and should be checked. NativeAd.Image logoImage = nativeContentAd.getLogo(); if (logoImage == null) { adView.getLogoView().setVisibility(View.INVISIBLE); } else { ((ImageView) adView.getLogoView()).setImageDrawable(logoImage.getDrawable()); adView.getLogoView().setVisibility(View.VISIBLE); } // Assign native ad object to the native view. adView.setNativeAd(nativeContentAd); } /** * Creates a request for a new native ad based on the boolean parameters and calls the * corresponding "populate" method when one is successfully returned. * * @param requestAppInstallAds indicates whether app install ads should be requested * @param requestContentAds indicates whether content ads should be requested */ private void refreshAd(boolean requestAppInstallAds, boolean requestContentAds) { if (!requestAppInstallAds && !requestContentAds) { Toast.makeText(this, "At least one ad format must be checked to request an ad.", Toast.LENGTH_SHORT).show(); return; } mRefresh.setEnabled(false); AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID); if (requestAppInstallAds) { builder.forAppInstallAd(new NativeAppInstallAd.OnAppInstallAdLoadedListener() { @Override public void onAppInstallAdLoaded(NativeAppInstallAd ad) { FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder); NativeAppInstallAdView adView = (NativeAppInstallAdView) getLayoutInflater() .inflate(R.layout.ad_app_install, null); populateAppInstallAdView(ad, adView); frameLayout.removeAllViews(); frameLayout.addView(adView); } }); } if (requestContentAds) { builder.forContentAd(new NativeContentAd.OnContentAdLoadedListener() { @Override public void onContentAdLoaded(NativeContentAd ad) { FrameLayout frameLayout = findViewById(R.id.fl_adplaceholder); NativeContentAdView adView = (NativeContentAdView) getLayoutInflater() .inflate(R.layout.ad_content, null); populateContentAdView(ad, adView); frameLayout.removeAllViews(); frameLayout.addView(adView); } }); } VideoOptions videoOptions = new VideoOptions.Builder() .setStartMuted(mStartVideoAdsMuted.isChecked()) .build(); NativeAdOptions adOptions = new NativeAdOptions.Builder() .setVideoOptions(videoOptions) .build(); builder.withNativeAdOptions(adOptions); AdLoader adLoader = builder.withAdListener(new AdListener() { @Override public void onAdFailedToLoad(int errorCode) { mRefresh.setEnabled(true); Toast.makeText(MainActivity.this, "Failed to load native ad: " + errorCode, Toast.LENGTH_SHORT).show(); } }).build(); adLoader.loadAd(new AdRequest.Builder().build()); mVideoStatus.setText(""); } }