1717package com .huawei .hms .ads .sdk ;
1818
1919import android .os .Bundle ;
20+ import android .util .Log ;
2021import android .view .View ;
22+ import android .view .ViewGroup ;
2123import android .widget .Button ;
2224import android .widget .ScrollView ;
2325import android .widget .RadioButton ;
24- import android .widget .TextView ;
2526import android .widget .Toast ;
2627
2728import com .huawei .hms .ads .AdListener ;
2829import com .huawei .hms .ads .AdParam ;
29- import com .huawei .hms .ads .VideoOperator ;
30+ import com .huawei .hms .ads .VideoConfiguration ;
3031import com .huawei .hms .ads .nativead .DislikeAdListener ;
31- import com .huawei .hms .ads .nativead .MediaView ;
3232import com .huawei .hms .ads .nativead .NativeAd ;
3333import com .huawei .hms .ads .nativead .NativeAdConfiguration ;
3434import com .huawei .hms .ads .nativead .NativeAdLoader ;
35- import com .huawei .hms .ads .nativead .NativeView ;
3635
3736public class NativeActivity extends BaseActivity {
38- private RadioButton small ;
39- private RadioButton video ;
37+ private static final String TAG = NativeActivity .class .getSimpleName ();
38+
39+ private RadioButton bigImage ;
40+ private RadioButton threeSmall ;
41+ private RadioButton smallImage ;
42+ private RadioButton videoWithText ;
43+ private RadioButton appDownloadBtn ;
44+
4045 private Button loadBtn ;
4146 private ScrollView adScrollView ;
4247
43- private int layoutId ;
4448 private NativeAd globalNativeAd ;
4549
46- private VideoOperator .VideoLifecycleListener videoLifecycleListener = new VideoOperator .VideoLifecycleListener () {
47- @ Override
48- public void onVideoStart () {
49- updateStatus (getString (R .string .status_play_start ), false );
50- }
51-
52- @ Override
53- public void onVideoPlay () {
54- updateStatus (getString (R .string .status_playing ), false );
55- }
56-
57- @ Override
58- public void onVideoEnd () {
59- // If there is a video, load a new native ad only after video playback is complete.
60- updateStatus (getString (R .string .status_play_end ), true );
61- }
62- };
63-
6450 @ Override
6551 protected void onCreate (Bundle savedInstanceState ) {
6652 super .onCreate (savedInstanceState );
6753 setTitle (getString (R .string .native_ad ));
6854 setContentView (R .layout .activity_native );
6955
70- small = findViewById (R .id .radio_button_small );
71- video = findViewById (R .id .radio_button_video );
56+ bigImage = findViewById (R .id .radio_button_large );
57+ threeSmall = findViewById (R .id .radio_button_three_small );
58+ smallImage = findViewById (R .id .radio_button_small );
59+ videoWithText = findViewById (R .id .radio_button_video );
60+ appDownloadBtn = findViewById (R .id .radio_button_app_download_button );
61+
7262 loadBtn = findViewById (R .id .btn_load );
7363 adScrollView = findViewById (R .id .scroll_view_ad );
7464
@@ -88,15 +78,18 @@ public void onClick(View view) {
8878 * @return ad slot ID
8979 */
9080 private String getAdId () {
91- String adId ;
92- layoutId = R .layout .native_video_template ;
93- if (small .isChecked ()) {
81+ String adId = getString (R .string .ad_id_native );
82+ ;
83+ if (bigImage .isChecked ()) {
84+ adId = getString (R .string .ad_id_native );
85+ } else if (smallImage .isChecked ()) {
9486 adId = getString (R .string .ad_id_native_small );
95- layoutId = R .layout .native_small_template ;
96- } else if (video .isChecked ()) {
87+ } else if (threeSmall .isChecked ()) {
88+ adId = getString (R .string .ad_id_native_three );
89+ } else if (videoWithText .isChecked ()) {
90+ adId = getString (R .string .ad_id_native_video );
91+ } else if (appDownloadBtn .isChecked ()) {
9792 adId = getString (R .string .ad_id_native_video );
98- } else {
99- adId = getString (R .string .ad_id_native );
10093 }
10194 return adId ;
10295 }
@@ -110,7 +103,6 @@ private void loadAd(String adId) {
110103 updateStatus (null , false );
111104
112105 NativeAdLoader .Builder builder = new NativeAdLoader .Builder (this , adId );
113-
114106 builder .setNativeAdLoadedListener (new NativeAd .NativeAdLoadedListener () {
115107 @ Override
116108 public void onNativeAdLoaded (NativeAd nativeAd ) {
@@ -121,19 +113,28 @@ public void onNativeAdLoaded(NativeAd nativeAd) {
121113 showNativeAd (nativeAd );
122114 }
123115 }).setAdListener (new AdListener () {
116+ @ Override
117+ public void onAdLoaded () {
118+ updateStatus (getString (R .string .status_load_ad_finish ), true );
119+ }
120+
124121 @ Override
125122 public void onAdFailed (int errorCode ) {
126123 // Call this method when an ad fails to be loaded.
127124 updateStatus (getString (R .string .status_load_ad_fail ) + errorCode , true );
128125 }
129126 });
130127
128+ VideoConfiguration videoConfiguration = new VideoConfiguration .Builder ()
129+ .setStartMuted (true )
130+ .build ();
131+
131132 NativeAdConfiguration adConfiguration = new NativeAdConfiguration .Builder ()
132133 .setChoicesPosition (NativeAdConfiguration .ChoicesPosition .BOTTOM_RIGHT ) // Set custom attributes.
134+ .setVideoConfiguration (videoConfiguration )
133135 .build ();
134136
135137 NativeAdLoader nativeAdLoader = builder .setNativeAdOptions (adConfiguration ).build ();
136-
137138 nativeAdLoader .loadAd (new AdParam .Builder ().build ());
138139
139140 updateStatus (getString (R .string .status_ad_loading ), false );
@@ -151,65 +152,51 @@ private void showNativeAd(NativeAd nativeAd) {
151152 }
152153 globalNativeAd = nativeAd ;
153154
154- // Obtain NativeView.
155- final NativeView nativeView = (NativeView ) getLayoutInflater ().inflate (layoutId , null );
156-
157- // Register and populate a native ad material view.
158- initNativeAdView (globalNativeAd , nativeView );
159- globalNativeAd .setDislikeAdListener (new DislikeAdListener () {
160- @ Override
161- public void onAdDisliked () {
162- // Call this method when an ad is closed.
163- updateStatus (getString (R .string .ad_is_closed ), true );
164- adScrollView .removeView (nativeView );
165- }
166- });
167-
168- // Add NativeView to the app UI.
169- adScrollView .removeAllViews ();
170- adScrollView .addView (nativeView );
155+ final View nativeView = createNativeView (nativeAd , adScrollView );
156+ if (nativeView != null ) {
157+ globalNativeAd .setDislikeAdListener (new DislikeAdListener () {
158+ @ Override
159+ public void onAdDisliked () {
160+ // Call this method when an ad is closed.
161+ updateStatus (getString (R .string .ad_is_closed ), true );
162+ adScrollView .removeView (nativeView );
163+ }
164+ });
165+
166+ // Add NativeView to the app UI.
167+ adScrollView .removeAllViews ();
168+ adScrollView .addView (nativeView );
169+ }
171170 }
172171
173172 /**
174- * Register and populate a native ad material view .
173+ * Create a nativeView by creativeType and fill in ad material.
175174 *
176175 * @param nativeAd native ad object that contains ad materials.
177- * @param nativeView native ad view to be populated into .
176+ * @param parentView parent view of nativeView .
178177 */
179- private void initNativeAdView (NativeAd nativeAd , NativeView nativeView ) {
180- // Register a native ad material view.
181- nativeView .setTitleView (nativeView .findViewById (R .id .ad_title ));
182- nativeView .setMediaView ((MediaView ) nativeView .findViewById (R .id .ad_media ));
183- nativeView .setAdSourceView (nativeView .findViewById (R .id .ad_source ));
184- nativeView .setCallToActionView (nativeView .findViewById (R .id .ad_call_to_action ));
185-
186- // Populate a native ad material view.
187- ((TextView ) nativeView .getTitleView ()).setText (nativeAd .getTitle ());
188- nativeView .getMediaView ().setMediaContent (nativeAd .getMediaContent ());
189-
190- if (null != nativeAd .getAdSource ()) {
191- ((TextView ) nativeView .getAdSourceView ()).setText (nativeAd .getAdSource ());
192- }
193- nativeView .getAdSourceView ()
194- .setVisibility (null != nativeAd .getAdSource () ? View .VISIBLE : View .INVISIBLE );
195-
196- if (null != nativeAd .getCallToAction ()) {
197- ((Button ) nativeView .getCallToActionView ()).setText (nativeAd .getCallToAction ());
198- }
199- nativeView .getCallToActionView ()
200- .setVisibility (null != nativeAd .getCallToAction () ? View .VISIBLE : View .INVISIBLE );
201-
202- // Obtain a video controller.
203- VideoOperator videoOperator = nativeAd .getVideoOperator ();
204-
205- // Check whether a native ad contains video materials.
206- if (videoOperator .hasVideo ()) {
207- // Add a video lifecycle event listener.
208- videoOperator .setVideoLifecycleListener (videoLifecycleListener );
178+ private View createNativeView (NativeAd nativeAd , ViewGroup parentView ) {
179+ int createType = nativeAd .getCreativeType ();
180+ Log .i (TAG , "Native ad createType is " + createType );
181+ if (createType == 2 || createType == 102 ) {
182+ // Large image
183+ return NativeViewFactory .createImageOnlyAdView (nativeAd , parentView );
184+ } else if (createType == 3 || createType == 6 ) {
185+ // Large image with text or video with text
186+ return NativeViewFactory .createMediumAdView (nativeAd , parentView );
187+ } else if (createType == 103 || createType == 106 ) {
188+ // Large image with text or Video with text, using AppDownloadButton template.
189+ return NativeViewFactory .createAppDownloadButtonAdView (nativeAd , parentView );
190+ } else if (createType == 7 || createType == 107 ) {
191+ // Small image with text-
192+ return NativeViewFactory .createSmallImageAdView (nativeAd , parentView );
193+ } else if (createType == 8 || createType == 108 ) {
194+ // Three small images with text
195+ return NativeViewFactory .createThreeImagesAdView (nativeAd , parentView );
196+ } else {
197+ // Undefined creative type
198+ return null ;
209199 }
210-
211- // Register a native ad object.
212- nativeView .setNativeAd (nativeAd );
213200 }
214201
215202 /**
0 commit comments