Skip to content

Commit cf64c85

Browse files
author
Andrew Brogdon
committed
Adds native video and an APIDemo update.
1 parent 5bc5737 commit cf64c85

File tree

19 files changed

+229
-52
lines changed

19 files changed

+229
-52
lines changed
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 24
5-
buildToolsVersion "23.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
88
applicationId "com.google.example.gms.nativeadvancedexample"
99
minSdkVersion 14
10-
targetSdkVersion 24
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -20,8 +20,8 @@ android {
2020
}
2121

2222
dependencies {
23-
compile 'com.android.support:appcompat-v7:24.2.0'
24-
compile 'com.google.firebase:firebase-ads:9.4.0'
23+
compile 'com.android.support:appcompat-v7:25.1.0'
24+
compile 'com.google.firebase:firebase-ads:10.0.1'
2525
}
2626

2727
apply plugin: 'com.google.gms.google-services'

admob/NativeAdvancedExample/app/src/main/java/com/google/example/gms/nativeadvancedexample/MainActivity.java

Lines changed: 62 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,23 @@
2626
import android.widget.RatingBar;
2727
import android.widget.TextView;
2828
import android.widget.Toast;
29+
2930
import com.google.android.gms.ads.AdListener;
3031
import com.google.android.gms.ads.AdLoader;
3132
import com.google.android.gms.ads.AdRequest;
3233
import com.google.android.gms.ads.MobileAds;
34+
import com.google.android.gms.ads.VideoController;
35+
import com.google.android.gms.ads.VideoOptions;
36+
import com.google.android.gms.ads.formats.MediaView;
3337
import com.google.android.gms.ads.formats.NativeAd;
38+
import com.google.android.gms.ads.formats.NativeAdOptions;
3439
import com.google.android.gms.ads.formats.NativeAppInstallAd;
3540
import com.google.android.gms.ads.formats.NativeAppInstallAdView;
3641
import com.google.android.gms.ads.formats.NativeContentAd;
3742
import com.google.android.gms.ads.formats.NativeContentAdView;
43+
3844
import java.util.List;
45+
import java.util.Locale;
3946

4047
/**
4148
* A simple activity class that displays native ad formats.
@@ -48,6 +55,8 @@ public class MainActivity extends ActionBarActivity {
4855
private Button mRefresh;
4956
private CheckBox mRequestAppInstallAds;
5057
private CheckBox mRequestContentAds;
58+
private CheckBox mStartVideoAdsMuted;
59+
private TextView mVideoStatus;
5160

5261
@Override
5362
protected void onCreate(Bundle savedInstanceState) {
@@ -60,6 +69,8 @@ protected void onCreate(Bundle savedInstanceState) {
6069
mRefresh = (Button) findViewById(R.id.btn_refresh);
6170
mRequestAppInstallAds = (CheckBox) findViewById(R.id.cb_appinstall);
6271
mRequestContentAds = (CheckBox) findViewById(R.id.cb_content);
72+
mStartVideoAdsMuted = (CheckBox) findViewById(R.id.cb_start_muted);
73+
mVideoStatus = (TextView) findViewById(R.id.tv_video_status);
6374

6475
mRefresh.setOnClickListener(new View.OnClickListener() {
6576
@Override
@@ -82,29 +93,54 @@ public void onClick(View v) {
8293
*/
8394
private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
8495
NativeAppInstallAdView adView) {
96+
// Get the video controller for the ad. One will always be provided, even if the ad doesn't
97+
// have a video asset.
98+
VideoController vc = nativeAppInstallAd.getVideoController();
99+
100+
// Create a new VideoLifecycleCallbacks object and pass it to the VideoController. The
101+
// VideoController will call methods on this object when events occur in the video
102+
// lifecycle.
103+
vc.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
104+
public void onVideoEnd() {
105+
// Publishers should allow native ads to complete video playback before refreshing
106+
// or replacing them with another ad in the same UI location.
107+
mRefresh.setEnabled(true);
108+
mVideoStatus.setText("Video status: Video playback has ended.");
109+
super.onVideoEnd();
110+
}
111+
});
112+
85113
adView.setHeadlineView(adView.findViewById(R.id.appinstall_headline));
86-
adView.setImageView(adView.findViewById(R.id.appinstall_image));
87114
adView.setBodyView(adView.findViewById(R.id.appinstall_body));
88115
adView.setCallToActionView(adView.findViewById(R.id.appinstall_call_to_action));
89116
adView.setIconView(adView.findViewById(R.id.appinstall_app_icon));
90117
adView.setPriceView(adView.findViewById(R.id.appinstall_price));
91118
adView.setStarRatingView(adView.findViewById(R.id.appinstall_stars));
92119
adView.setStoreView(adView.findViewById(R.id.appinstall_store));
93120

121+
// The MediaView will display a video asset if one is present in the ad, and the first image
122+
// asset otherwise.
123+
MediaView mediaView = (MediaView) adView.findViewById(R.id.appinstall_media);
124+
adView.setMediaView(mediaView);
125+
94126
// Some assets are guaranteed to be in every NativeAppInstallAd.
95127
((TextView) adView.getHeadlineView()).setText(nativeAppInstallAd.getHeadline());
96128
((TextView) adView.getBodyView()).setText(nativeAppInstallAd.getBody());
97129
((Button) adView.getCallToActionView()).setText(nativeAppInstallAd.getCallToAction());
98-
((ImageView) adView.getIconView()).setImageDrawable(nativeAppInstallAd.getIcon()
99-
.getDrawable());
100130

101-
List<NativeAd.Image> images = nativeAppInstallAd.getImages();
102-
103-
if (images.size() > 0) {
104-
((ImageView) adView.getImageView()).setImageDrawable(images.get(0).getDrawable());
131+
// Apps can check the VideoController's hasVideoContent property to determine if the
132+
// NativeAppInstallAd has a video asset.
133+
if (vc.hasVideoContent()) {
134+
mVideoStatus.setText(String.format(Locale.getDefault(),
135+
"Video status: Ad contains a %.2f:1 video asset.",
136+
vc.getAspectRatio()));
137+
} else {
138+
mRefresh.setEnabled(true);
139+
mVideoStatus.setText("Video status: Ad does not contain a video asset.");
105140
}
106141

107-
// Some aren't guaranteed, however, and should be checked.
142+
// These assets aren't guaranteed to be in every NativeAppInstallAd, so it's important to
143+
// check before trying to display them.
108144
if (nativeAppInstallAd.getPrice() == null) {
109145
adView.getPriceView().setVisibility(View.INVISIBLE);
110146
} else {
@@ -140,6 +176,9 @@ private void populateAppInstallAdView(NativeAppInstallAd nativeAppInstallAd,
140176
*/
141177
private void populateContentAdView(NativeContentAd nativeContentAd,
142178
NativeContentAdView adView) {
179+
mVideoStatus.setText("Video status: Ad does not contain a video asset.");
180+
mRefresh.setEnabled(true);
181+
143182
adView.setHeadlineView(adView.findViewById(R.id.contentad_headline));
144183
adView.setImageView(adView.findViewById(R.id.contentad_image));
145184
adView.setBodyView(adView.findViewById(R.id.contentad_body));
@@ -187,6 +226,8 @@ private void refreshAd(boolean requestAppInstallAds, boolean requestContentAds)
187226
return;
188227
}
189228

229+
mRefresh.setEnabled(false);
230+
190231
AdLoader.Builder builder = new AdLoader.Builder(this, ADMOB_AD_UNIT_ID);
191232

192233
if (requestAppInstallAds) {
@@ -219,14 +260,27 @@ public void onContentAdLoaded(NativeContentAd ad) {
219260
});
220261
}
221262

263+
VideoOptions videoOptions = new VideoOptions.Builder()
264+
.setStartMuted(mStartVideoAdsMuted.isChecked())
265+
.build();
266+
267+
NativeAdOptions adOptions = new NativeAdOptions.Builder()
268+
.setVideoOptions(videoOptions)
269+
.build();
270+
271+
builder.withNativeAdOptions(adOptions);
272+
222273
AdLoader adLoader = builder.withAdListener(new AdListener() {
223274
@Override
224275
public void onAdFailedToLoad(int errorCode) {
276+
mRefresh.setEnabled(true);
225277
Toast.makeText(MainActivity.this, "Failed to load native ad: "
226278
+ errorCode, Toast.LENGTH_SHORT).show();
227279
}
228280
}).build();
229281

230282
adLoader.loadAd(new AdRequest.Builder().build());
283+
284+
mVideoStatus.setText("");
231285
}
232286
}

admob/NativeAdvancedExample/app/src/main/res/layout/activity_main.xml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,21 @@
2121
android:layout_height="wrap_content"
2222
android:layout_marginTop="16dp"
2323
android:checked="true"
24-
android:text="Request App Install Ads" />
24+
android:text="Request app install ads" />
2525

2626
<CheckBox
2727
android:id="@+id/cb_content"
2828
android:layout_width="wrap_content"
2929
android:layout_height="wrap_content"
3030
android:checked="true"
31-
android:text="Request Content Ads" />
31+
android:text="Request content ads" />
32+
33+
<CheckBox
34+
android:id="@+id/cb_start_muted"
35+
android:layout_width="wrap_content"
36+
android:layout_height="wrap_content"
37+
android:checked="true"
38+
android:text="Start video ads muted" />
3239

3340
<Button
3441
android:id="@+id/btn_refresh"
@@ -39,6 +46,15 @@
3946
android:layout_marginTop="16dp"
4047
android:text="Refresh Ad" />
4148

49+
<TextView
50+
android:text="Video status: "
51+
android:layout_width="match_parent"
52+
android:layout_height="wrap_content"
53+
android:layout_marginLeft="@dimen/activity_horizontal_margin"
54+
android:layout_marginRight="@dimen/activity_horizontal_margin"
55+
android:id="@+id/tv_video_status"
56+
android:textAppearance="@style/TextAppearance.AppCompat.Medium" />
57+
4258

4359
</LinearLayout>
4460
</ScrollView>

admob/NativeAdvancedExample/app/src/main/res/layout/ad_app_install.xml

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
</LinearLayout>
5555

5656
<LinearLayout
57-
android:layout_width="wrap_content"
57+
android:layout_width="match_parent"
5858
android:layout_height="wrap_content"
5959
android:orientation="vertical">
6060

@@ -63,20 +63,20 @@
6363
android:layout_width="wrap_content"
6464
android:layout_height="wrap_content"
6565
android:layout_marginRight="20dp"
66+
android:layout_marginEnd="20dp"
6667
android:textSize="12sp" />
6768

68-
<ImageView
69-
android:id="@+id/appinstall_image"
70-
android:layout_width="match_parent"
71-
android:layout_height="wrap_content"
72-
android:adjustViewBounds="true"
73-
android:maxHeight="150dp"
74-
android:paddingTop="5dp" />
69+
<com.google.android.gms.ads.formats.MediaView
70+
android:id="@+id/appinstall_media"
71+
android:layout_gravity="center_horizontal"
72+
android:layout_width="250dp"
73+
android:layout_height="175dp"
74+
android:layout_marginTop="5dp" />
7575

7676
<LinearLayout
7777
android:layout_width="wrap_content"
7878
android:layout_height="wrap_content"
79-
android:layout_gravity="right"
79+
android:layout_gravity="end"
8080
android:orientation="horizontal"
8181
android:paddingBottom="10dp"
8282
android:paddingTop="10dp">
@@ -85,14 +85,20 @@
8585
android:id="@+id/appinstall_price"
8686
android:layout_width="wrap_content"
8787
android:layout_height="wrap_content"
88+
android:paddingLeft="5dp"
89+
android:paddingStart="5dp"
8890
android:paddingRight="5dp"
91+
android:paddingEnd="5dp"
8992
android:textSize="12sp" />
9093

9194
<TextView
9295
android:id="@+id/appinstall_store"
9396
android:layout_width="wrap_content"
9497
android:layout_height="wrap_content"
98+
android:paddingLeft="5dp"
99+
android:paddingStart="5dp"
95100
android:paddingRight="5dp"
101+
android:paddingEnd="5dp"
96102
android:textSize="12sp" />
97103

98104
<Button

admob/NativeAdvancedExample/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ buildscript {
55
jcenter()
66
}
77
dependencies {
8-
classpath 'com.android.tools.build:gradle:2.1.2'
8+
classpath 'com.android.tools.build:gradle:2.2.3'
99
classpath 'com.google.gms:google-services:3.0.0'
1010
}
1111
}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
#Mon Nov 28 15:28:26 PST 2016
12
distributionBase=GRADLE_USER_HOME
23
distributionPath=wrapper/dists
34
zipStoreBase=GRADLE_USER_HOME
45
zipStorePath=wrapper/dists
5-
distributionUrl=https\://services.gradle.org/distributions/gradle-2.10-all.zip
6+
distributionUrl=https\://services.gradle.org/distributions/gradle-2.14.1-all.zip
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
apply plugin: 'com.android.application'
22

33
android {
4-
compileSdkVersion 23
5-
buildToolsVersion "23.0.2"
4+
compileSdkVersion 25
5+
buildToolsVersion "25.0.2"
66

77
defaultConfig {
88
applicationId "com.google.example.gms.ads.nativeexpressexample"
99
minSdkVersion 14
10-
targetSdkVersion 23
10+
targetSdkVersion 25
1111
versionCode 1
1212
versionName "1.0"
1313
}
@@ -20,8 +20,8 @@ android {
2020
}
2121

2222
dependencies {
23-
compile 'com.android.support:appcompat-v7:23.4.0'
24-
compile 'com.google.firebase:firebase-ads:9.0.0'
23+
compile 'com.android.support:appcompat-v7:25.1.0'
24+
compile 'com.google.firebase:firebase-ads:10.0.1'
2525
}
2626

2727
apply plugin: 'com.google.gms.google-services'

admob/NativeExpressExample/app/src/main/java/com/google/example/gms/ads/nativeexpressexample/MainActivity.java

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,63 @@
1818

1919
import android.os.Bundle;
2020
import android.support.v7.app.AppCompatActivity;
21+
import android.util.Log;
2122

23+
import com.google.android.gms.ads.AdListener;
2224
import com.google.android.gms.ads.AdRequest;
2325
import com.google.android.gms.ads.NativeExpressAdView;
26+
import com.google.android.gms.ads.VideoController;
27+
import com.google.android.gms.ads.VideoOptions;
2428

2529

2630
/**
2731
* A simple activity showing the use of a {@link NativeExpressAdView}.
2832
*/
2933
public class MainActivity extends AppCompatActivity {
3034

35+
private static String LOG_TAG = "EXAMPLE";
36+
37+
NativeExpressAdView mAdView;
38+
VideoController mVideoController;
39+
3140
@Override
3241
protected void onCreate(Bundle savedInstanceState) {
3342
super.onCreate(savedInstanceState);
3443
setContentView(R.layout.activity_main);
3544

36-
NativeExpressAdView adView = (NativeExpressAdView) findViewById(R.id.adView);
37-
adView.loadAd(new AdRequest.Builder().build());
45+
// Locate the NativeExpressAdView.
46+
mAdView = (NativeExpressAdView) findViewById(R.id.adView);
47+
48+
// Set its video options.
49+
mAdView.setVideoOptions(new VideoOptions.Builder()
50+
.setStartMuted(true)
51+
.build());
52+
53+
// The VideoController can be used to get lifecycle events and info about an ad's video
54+
// asset. One will always be returned by getVideoController, even if the ad has no video
55+
// asset.
56+
mVideoController = mAdView.getVideoController();
57+
mVideoController.setVideoLifecycleCallbacks(new VideoController.VideoLifecycleCallbacks() {
58+
@Override
59+
public void onVideoEnd() {
60+
Log.d(LOG_TAG, "Video playback is finished.");
61+
super.onVideoEnd();
62+
}
63+
});
64+
65+
// Set an AdListener for the AdView, so the Activity can take action when an ad has finished
66+
// loading.
67+
mAdView.setAdListener(new AdListener() {
68+
@Override
69+
public void onAdLoaded() {
70+
if (mVideoController.hasVideoContent()) {
71+
Log.d(LOG_TAG, "Received an ad that contains a video asset.");
72+
} else {
73+
Log.d(LOG_TAG, "Received an ad that does not contain a video asset.");
74+
}
75+
}
76+
});
77+
78+
mAdView.loadAd(new AdRequest.Builder().build());
3879
}
3980
}

admob/NativeExpressExample/app/src/main/res/layout/activity_main.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
android:layout_height="wrap_content"
2222
android:layout_centerHorizontal="true"
2323
android:layout_alignParentBottom="true"
24-
ads:adSize="320x150"
24+
ads:adSize="320x300"
2525
ads:adUnitId="@string/ad_unit_id">
2626
</com.google.android.gms.ads.NativeExpressAdView>
2727
</RelativeLayout>

0 commit comments

Comments
 (0)