Alternatively, you can use getter functions to get Ad Elements and then render Native Ad. Once the native ad is cached, success callbacks is received as adViewDidCacheAd() in sdk version below 3.6.0 and onAdReady() in sdk version 3.6.0 onwards.
To Fetch Native Ad always use only cacheAd().
showAd() or loadAd() apis are not supported for invoking Native Ads.
For your reference purpose, We have created VMAX Sample App for Native adspot. This app is open source and uploaded on Github where you can download and read example code for requesting Native Ads
1) Click here to download Native Content Stream Recycler View Demo App
2) Click here to download Native Content Stream List View Demo App
3) Click here to download Native Infeed List View Demo App
4) Click here to download Native Infeed Recycler View Demo App
Note: For all ad partners except AdMob follow below table.
Parameter | Recommended | Description |
nativeAd.getTitle(); | Yes | the title property of the ad |
nativeAd.getDesc(); | Yes | the description property of the ad |
nativeAd.getCtaText(); | Yes | the call to action title property of the ad |
nativeAd.getRating(); | Optional | |
nativeAd.getMediaView(); | Yes | to be used only for Facebook and Vserv Ads |
nativeAd.getNativeAdPartner(); | Yes | to get Ad partner |
nativeAd.getAdChoiceUrl(); | Yes | to be used only for Facebook |
nativeAd.getObjective(); | Optional | |
nativeAd.getVastVideoTag(); | Optional | |
nativeAd.getDesc2(); | Optional | |
nativeAd.getPrice(); | Optional | |
nativeAd.getDownloads(); | Optional | |
nativeAd.getAdChoiceView(); | Yes | getAdChoiceView() used only for Facebook. While using this Adchoice view of Facebook than ignore getAdChoiceUrl().It automatically handles click on it. |
nativeAd.getNativeAdType(); | Optional | |
nativeAd.getSalePrice(); | Optional | |
nativeAd.getSponsored(); | Optional | |
nativeAd.getLikes(); | Optional | |
nativeAd.getPhone(); | Optional | |
nativeAd.getAddress(); | Optional | |
nativeAd.getDisplayurl(); | Optional | |
nativeAd.getIcon().getUrl(); | Yes | |
nativeAd.getIcon().getWidth(); | ||
nativeAd.getIcon().getHeight(); | ||
nativeAd.getImageMain().getUrl(); | ||
nativeAd.getImageMain().getWidth(); | ||
nativeAd.getImageMain().getHeight(); | ||
nativeAd.getImageMedium().getUrl(); | ||
nativeAd.getImageMedium().getWidth(); | ||
nativeAd.getImageMedium().getHeight(); | ||
nativeAd.getImageTile().getUrl(); | ||
nativeAd.getImageTile().getWidth(); | ||
nativeAd.getImageTile().getHeight(); | ||
nativeAd.getImageAdChoice().getUrl(); | ||
nativeAd.getImageAdChoice().getWidth(); | ||
nativeAd.getImageAdChoice().getHeight(); |
Additional notes for InMobi Native Ads
From InMobi version 6.0.0 (supported from VMAX Android SDK v3.5.19), InMobi returns a ‘screenshots’ element on their dashboard as shown in the accompanying image.
1 2 3 4 5 6 |
screenshots = { aspectRatio = “1.91”; height = 313; url = “http://xyz.jpeg”; width = 600; }; |
- If you select an image with aspectRatio ‘1.91’, this is mapped to the ‘imageMain’ element of the Native Ad object. Thus, requesting for imageMedium will result in a nil value.
- If you select an image with aspectRatio ‘1’, this is mapped to the ‘imageMedium’ & ‘imageTile’ elements of the Native Ad object. Thus, requesting for imageMain will result in a nil value.
As a best practice, you should always check if the requested element is nil or not and for non-nil, use the native ad element in rendering your custom native ad layout.
Ad Network Policies
In addition, you need to adhere to the policies shared by AdMob and Facebook here:
- https://support.google.com/admob/answer/6240618
- https://support.google.com/admob/answer/6240809
- https://developers.facebook.com/docs/audience-network/guidelines/native-ads
If using any ad network other than AdMob or Facebook, be sure to include an Ad symbol so that app users can identify that it’s an ad. The below example shows a test ad from VMAX having an icon, large image, title, CTA button and the ad attribution symbol in the top right.
Once all elements are assigned to Ad parentview and just when your Ad parentView is visible use below function to track impressions and clicks. No need to set any listener on Ad. This function is very important used only when native Ads rendering is without helper libraries.
// very important function used only when native Ads render without helper libraries.
Implementation
Add vmax_custom_native_layout.xml in your Layouts
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 |
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/vmax_nativeborder"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top|center" android:gravity="top|center" android:orientation="vertical" android:visibility="visible"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_gravity="top|center" android:layout_marginBottom="1dp" android:layout_marginLeft="1dp" android:layout_marginRight="1dp" android:gravity="top" android:orientation="horizontal" android:visibility="visible"> <ImageView android:id="@+id/vmax_custom_icon" android:layout_width="48dp" android:layout_height="48dp" android:layout_gravity="left" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="top|center" android:gravity="top|center" android:orientation="vertical" android:weightSum="1.0"> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_weight="0.7" android:orientation="horizontal"> <TextView android:id="@+id/vmax_custom_title" android:layout_width="234dp" android:layout_height="24dp" android:ellipsize="end" android:maxLines="1" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="Title" android:textColor="@color/vmax_black" android:textSize="12sp" android:textStyle="bold" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="right" android:gravity="right" android:visibility="gone" android:orientation="horizontal"> <TextView android:id="@+id/vmax_sponsored" android:layout_width="15dp" android:layout_height="15dp" android:layout_gravity="center" android:layout_marginRight="2dp" android:background="@drawable/vmax_nativebg_ad" android:gravity="center" android:paddingLeft="2dp" android:paddingRight="2dp" android:text="@string/vmax_advertisment_text" android:textColor="@color/vmax_black" android:textSize="8sp" android:visibility="visible" /> <FrameLayout android:id="@+id/vmax_custom_adChoiceView" android:layout_width="15dp" android:layout_height="15dp" android:layout_gravity="center" android:orientation="vertical" android:gravity="top|right" android:paddingLeft="1dp" android:paddingRight="1dp" android:visibility="visible"> </FrameLayout> </LinearLayout> </LinearLayout> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|center" android:layout_weight="0.3" android:gravity="bottom|center" android:orientation="horizontal"> <TextView android:id="@+id/vmax_custom_desc" android:layout_width="188dp" android:layout_height="24dp" android:layout_marginTop="1sp" android:ellipsize="end" android:lines="2" android:paddingLeft="8dp" android:paddingRight="8dp" android:text="Description" android:textColor="@color/vmax_black" android:textSize="8sp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:gravity="center" android:orientation="horizontal"> <Button android:id="@+id/vmax_custom_cta" style="@style/style_vmax_button" android:layout_width="82dp" android:layout_height="24dp" android:layout_gravity="right" android:gravity="center" android:lines="1" android:paddingLeft="1dp" android:paddingRight="1dp" android:text="Install Now" android:textAlignment="center" /> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout> <ImageView android:id="@+id/vmax_custom_otherimg" android:layout_width="match_parent" android:layout_height="200dp" android:layout_gravity="bottom|center" android:gravity="bottom|center" android:src="@drawable/vmax_logo" android:visibility="visible" /> <RelativeLayout android:id="@+id/vmax_custom_media_view" android:layout_width="320dp" android:layout_height="200dp" android:layout_gravity="bottom|center" android:gravity="bottom|center" android:visibility="gone"> </RelativeLayout> </LinearLayout> </RelativeLayout> |
Add below code in your Activity class
SDK 3.6.0 onwards
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout nativecustomLayout = (RelativeLayout) inflater .inflate(context.getResources().getIdentifier("vmax_custom_native_layout", "layout", context.getPackageName()), null); VmaxAdView vmaxAdView = new VmaxAdView(this, "", VmaxAdView.UX_NATIVE); VmaxAdSettings vmaxAdSettings = new VmaxAdSettings(); vmaxAdSettings.setAdmobNativeExpressAdSize(Constants.NativeAdSize.NATIVE_AD_SIZE_FULL_WIDTH, 200); vmaxAdView.setAdSettings(vmaxAdSettings); vmaxAdView.setAdListener(new VmaxAdListener() { @Override public void onAdError(VmaxAdError error) {} @Override public void onAdReady(VmaxAdView adView) { Toast.makeText(MainActivity.this, "Ad Cached", Toast.LENGTH_SHORT).show(); final NativeAd nativeAd = adView.getNativeAd(); nativecustomLayout.removeAllViews(); if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_ADMOB) && nativeAd.getNativeAdType().equalsIgnoreCase(Constants.NativeAdType.VMAX_ADMOB_EXPRESS_AD)) { //To render Admob Express Ads nativecustomLayout.setVisibility(View.VISIBLE); nativeAd.registerViewForInteraction(adView, nativecustomLayout, nativecustomLayout, null); } else { HashSet < NativeImageDownload > nativeImageElementSet = new HashSet < > (); LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE); RelativeLayout fl_native = (RelativeLayout) inflater.inflate(getResources().getIdentifier("vmax_custom_native_layout", "layout", getPackageName ()), null); List < View > listview = new ArrayList < > (); //To download Ad Icon if (nativeAd.getIcon() != null) { ImageView iconView = (ImageView) fl_native.findViewById(R.id.vmax_custom_icon); if (nativeAd.getIcon().getUrl() != null && !TextUtils.isEmpty(nativeAd.getIcon().getUrl())) { String iconURL = nativeAd.getIcon().getUrl(); Log.d("vmax", "icon URL=" + iconURL); nativeImageElementSet.add(new NativeImageDownload(iconURL, iconView, 48, 48)); } listview.add(iconView); } //To fetch Ad Title if (nativeAd.getTitle() != null) { TextView titleView = (TextView) fl_native.findViewById(R.id.vmax_custom_title); titleView.setText(nativeAd.getTitle()); listview.add(titleView); } //To fetch Ad Description if (nativeAd.getDesc() != null) { TextView descView = (TextView) fl_native.findViewById(R.id.vmax_custom_desc); descView.setText(nativeAd.getDesc()); listview.add(descView); } //To fetch CTA Text if (nativeAd.getCtaText() != null) { Button ctaNative = (Button) fl_native.findViewById(R.id.vmax_custom_cta); ctaNative.setText(nativeAd.getCtaText()); listview.add(ctaNative); } RelativeLayout media_layout = (RelativeLayout) fl_native.findViewById(R.id.vmax_custom_media_view); ImageView otherImageView = (ImageView) fl_native.findViewById(R.id.vmax_custom_otherimg); if (nativeAd.getMediaView() != null && nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_FACEBOOK)) { //To render Facebook MediaView MediaView fbMediaView = (MediaView) nativeAd.getMediaView(); media_layout.removeAllViews(); media_layout.addView(fbMediaView); media_layout.setVisibility(View.VISIBLE); otherImageView.setVisibility(View.GONE); } else if (nativeAd.getMediaView() != null && nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_NATIVE_AD)) { //To render Vmax MediaView VmaxNativeMediaView vmaxNativeMediaView = (VmaxNativeMediaView) nativeAd.getMediaView(); media_layout.removeAllViews(); media_layout.addView(vmaxNativeMediaView); media_layout.setVisibility(View.VISIBLE); otherImageView.setVisibility(View.GONE); } else { //To render other Vmax Static Ad elements media_layout.removeAllViews(); media_layout.setVisibility(View.GONE); otherImageView.setVisibility(View.VISIBLE); VmaxImage imageMain = nativeAd.getImageMain(); VmaxImage imageMedium = nativeAd.getImageMedium(); VmaxImage imageTile = nativeAd.getImageTile(); VmaxImage imageBanner = nativeAd.getImageBanner(); if (imageMain != null && imageMain.getUrl() != null && !TextUtils.isEmpty(imageMain.getUrl())) { String otherImageURL = imageMain.getUrl(); Log.d("vmax", "main URL=" + otherImageURL); nativeImageElementSet.add(new NativeImageDownload(otherImageURL, otherImageView, 320, 200)); } else if (imageMedium != null && imageMedium.getUrl() != null && !TextUtils.isEmpty(imageMedium.getUrl())) { String otherImageURL = imageMedium.getUrl(); Log.d("vmax", "medium URL=" + otherImageURL); nativeImageElementSet.add(new NativeImageDownload(otherImageURL, otherImageView, 320, 200)); } else if (imageTile != null && imageTile.getUrl() != null && !TextUtils.isEmpty(imageTile.getUrl())) { String otherImageURL = imageTile.getUrl(); Log.d("vmax", "tile URL=" + otherImageURL); nativeImageElementSet.add(new NativeImageDownload(otherImageURL, otherImageView, 320, 200)); } else if (imageBanner != null && imageBanner.getUrl() != null && !TextUtils.isEmpty(imageBanner.getUrl())) { String otherImageURL = imageBanner.getUrl(); Log.d("vmax", "banner URL=" + otherImageURL); nativeImageElementSet.add(new NativeImageDownload(otherImageURL, otherImageView, 320, 200)); } listview.add(otherImageView); } ImageLoader imageLoader = new ImageLoader(nativeImageElementSet); //setNativeImageDownloadListener() is optional, used to get notified when all image elements are successfully downloaded imageLoader.setNativeImageDownloadListener(new NativeImageDownloadListener() { @Override public void onTaskDone() {} @Override public void onTaskError() {} }); imageLoader.execute(); FrameLayout adchoiceViewLayout = (FrameLayout) fl_native.findViewById(R.id.vmax_custom_adChoiceView); if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_FACEBOOK)) { //To Download AdChoice icon in case of Facebook Native Ad if (nativeAd.getImageAdChoice() != null) { ImageView adChoiceView = new ImageView(MainVmaxActivity.this); if (nativeAd.getImageAdChoice().getUrl() != null && !TextUtils.isEmpty(nativeAd.getImageAdChoice().getUrl())) { String adchoiceImageURL = nativeAd.getImageAdChoice().getUrl(); HashSet < NativeImageDownload > imageSet = new HashSet < > (); imageSet.add(new NativeImageDownload(adchoiceImageURL, adChoiceView, 15, 15)); ImageLoader imageLoader = new ImageLoader(imageSet); //setNativeImageDownloadListener() is optional, used to get notified when all image elements are sucessfully downloaded imageLoader.setNativeImageDownloadListener(new NativeImageDownloadListener() { @Override public void onTaskDone() { } @Override public void onTaskError() { } }); imageLoader.execute(); } adChoiceView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String adChoiceActionUrl = nativeAd.getAdChoiceUrl(); Uri uri = Uri.parse(adChoiceActionUrl); Intent intent = new Intent(Intent.ACTION_VIEW, uri); MainVmaxActivity.this.startActivity(intent); } }); adChoiceView.setVisibility(View.VISIBLE); adchoiceViewLayout.addView(adChoiceView); adchoiceViewLayout.setVisibility(View.VISIBLE); } } else if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_ADMOB)) { //To Download App install Icon in case of Admob Ad adchoiceViewLayout.removeAllViews(); if (nativeAd.getAdChoiceView() != null) { if (nativeAd.getAdChoiceView() instanceof NativeAppInstallAdView) { NativeAppInstallAdView nativeAppInstallAdView = (NativeAppInstallAdView) nativeAd.getAdChoiceView(); adchoiceViewLayout.addView(nativeAppInstallAdView); } else if (nativeAd.getAdChoiceView() instanceof NativeContentAdView) { NativeContentAdView nativeContentAdView = (NativeContentAdView) nativeAd.getAdChoiceView(); adchoiceViewLayout.addView(nativeContentAdView); } adchoiceViewLayout.setVisibility(View.VISIBLE); } } else { adchoiceViewLayout.removeAllViews(); adchoiceViewLayout.setVisibility(View.GONE); } ll_native.addView(fl_native); ll_native.setVisibility(View.VISIBLE); //To register Clicks nativeAd.registerViewForInteraction(adView, fl_native, fl_native, listview); } } @Override public void onAdDismissed(VmaxAdView adView) {} @Override public void onAdEnd(boolean isVideoCompleted, long reward) {} }); vmaxAdView.cacheAd(); |
1 |
vmaxNativeAd.registerViewForInteraction(VmaxAdView adView, RelativeLayout vgroup, View view, List listview); |
adView is mandatory for proper handling of Native Ads.
vgroup is main Ad parnetView and is compulsory to be of type RelativeLayout.
view is mandatory so as to set listener, it should be CTA view or any other Native Ad element view.
listview is Optional if click on multiple views required. If listview is provided than third argument View view should be parent of the view available in list.
Note: For AdMob AdChoice View icon, pass second parameter in above function that is Ad container Layout and keep top right space empty say 15px. VMAX SDK will handle to render AdMob Adchoice icon for AdMob.
Before SDK 3.6.0
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 |
LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); FrameLayout nativecustomLayout = (FrameLayout) inflater .inflate(context.getResources().getIdentifier("vmax_custom_native_layout", "layout", context.getPackageName()), null); VmaxAdView vmaxAdView = new VmaxAdView(this, "<Adspot Id>", VmaxAdView.UX_NATIVE); Map tempAdSettings = new HashMap<>(); tempAdSettings.put(VmaxAdSettings.AdSettings_admob_nativeAdSize, "360x320"); //Any custom size as per Template created on Admob Dashboard vmaxAdView.setAdSettings(tempAdSettings); vmaxAdView.setAdListener(new VmaxAdListener() { @Override public VmaxAdView didFailedToLoadAd(String s) { return null; } @Override public VmaxAdView didFailedToCacheAd(String s) { return null; } @Override public void adViewDidLoadAd(VmaxAdView adView) { } @Override public void adViewDidCacheAd(VmaxAdView adView) { Toast.makeText(MainActivity.this, "Ad Cached", Toast.LENGTH_SHORT).show(); final NativeAd nativeAd = adView.getNativeAd(); nativeAdLayout.removeAllViews(); nativecustomLayout.removeAllViews(); if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_ADMOB) && nativeAd.getNativeAdType().equalsIgnoreCase(Constants.NativeAdType.VMAX_ADMOB_EXPRESS_AD)) { //To render Admob Express Ads nativeAdLayout.setVisibility(View.VISIBLE); nativeAd.registerViewForInteraction(adView, nativeAdLayout, nativeAdLayout, null); } else { //To render Fb/Inmobi/Vserv Ads LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); nativecustomLayout = (FrameLayout) inflater .inflate(context.getResources().getIdentifier("vmax_custom_native_layout", "layout", context.getPackageName()), null); List<View> listview = new ArrayList<>(); if (nativeAd.getIcon() != null && nativeAd.getIcon().getImageView() != null) { ImageView iconView = (ImageView) nativecustomLayout.findViewById(R.id.vmax_custom_icon); iconView.setImageDrawable(nativeAd.getIcon().getImageView().getDrawable()); listview.add(iconView); } if (nativeAd.getTitle() != null) { TextView titleView = (TextView) nativecustomLayout.findViewById(R.id.vmax_custom_title); titleView.setText(nativeAd.getTitle()); listview.add(titleView); } if (nativeAd.getDesc() != null) { TextView descView = (TextView) nativecustomLayout.findViewById(R.id.vmax_custom_desc); descView.setText(nativeAd.getDesc()); listview.add(descView); } if (nativeAd.getCtaText() != null) { Button ctaNative = (Button) nativecustomLayout.findViewById(R.id.vmax_custom_cta); ctaNative.setText(nativeAd.getCtaText()); listview.add(ctaNative); } RelativeLayout media_layout = (RelativeLayout) nativecustomLayout.findViewById(R.id.vmax_custom_media_view); ImageView otherImageView = (ImageView) nativecustomLayout.findViewById(R.id.vmax_custom_otherimg); if (nativeAd.getMediaView() != null && nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_FACEBOOK)) { //MediaView of Facebook is used to render videos for FB Ads MediaView fbMediaView = (MediaView) nativeAd.getMediaView(); if (fbMediaView != null) { ViewGroup parent = (ViewGroup) fbMediaView.getParent(); if (parent != null) { parent.removeView(fbMediaView); } } media_layout.removeAllViews(); media_layout.addView(fbMediaView); media_layout.setVisibility(View.VISIBLE); otherImageView.setVisibility(View.GONE); } else if (nativeAd.getMediaView() != null && nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_NATIVE_AD)) { //VmaxNativeMediaView is used to render Video in Native Ads VmaxNativeMediaView vmaxNativeMediaView = (VmaxNativeMediaView) nativeAd.getMediaView(); media_layout.removeAllViews(); media_layout.addView(vmaxNativeMediaView); media_layout.setVisibility(View.VISIBLE); otherImageView.setVisibility(View.GONE); } else { media_layout.removeAllViews(); media_layout.setVisibility(View.GONE); otherImageView.setVisibility(View.VISIBLE); if (nativeAd.getImageMain() != null && nativeAd.getImageMain().getImageView() != null) { otherImageView.setImageDrawable(nativeAd.getImageMain().getImageView().getDrawable()); } else if (nativeAd.getImageMedium() != null && nativeAd.getImageMedium().getImageView() != null) { otherImageView.setImageDrawable(nativeAd.getImageMedium().getImageView().getDrawable()); } else if (nativeAd.getImageTile() != null && nativeAd.getImageTile().getImageView() != null) { otherImageView.setImageDrawable(nativeAd.getImageTile().getImageView().getDrawable()); } else if (nativeAd.getImageBanner() != null && nativeAd.getImageBanner().getImageView() != null) { otherImageView.setImageDrawable(nativeAd.getImageBanner().getImageView().getDrawable()); } listview.add(otherImageView); } FrameLayout adchoiceViewLayout = (FrameLayout) nativecustomLayout.findViewById(R.id.vmax_custom_adChoiceView); if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_FACEBOOK)) { if (nativeAd.getImageAdChoice() != null && nativeAd.getImageAdChoice().getImageView() != null) { ImageView adChoiceView = new ImageView(context); adChoiceView.setImageDrawable(nativeAd.getImageAdChoice().getImageView().getDrawable()); adChoiceView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { String adChoiceActionUrl = nativeAd.getAdChoiceUrl(); Uri uri = Uri.parse(adChoiceActionUrl); Intent intent = new Intent(Intent.ACTION_VIEW, uri); context.startActivity(intent); } }); adChoiceView.setVisibility(View.VISIBLE); adchoiceViewLayout.addView(adChoiceView); adchoiceViewLayout.setVisibility(View.VISIBLE); } } else if (nativeAd.getNativeAdPartner() != null && nativeAd.getNativeAdPartner().equals(Constants.AdPartner.VMAX_ADMOB)) { adchoiceViewLayout.removeAllViews(); if (nativeAd.getAdChoiceView() != null) { if (nativeAd.getAdChoiceView() instanceof NativeAppInstallAdView) { NativeAppInstallAdView nativeAppInstallAdView = (NativeAppInstallAdView) nativeAd.getAdChoiceView(); adchoiceViewLayout.addView(nativeAppInstallAdView); } else if (nativeAd.getAdChoiceView() instanceof NativeContentAdView) { NativeContentAdView nativeContentAdView = (NativeContentAdView) nativeAd.getAdChoiceView(); adchoiceViewLayout.addView(nativeContentAdView); } adchoiceViewLayout.setVisibility(View.VISIBLE); } } else { adchoiceViewLayout.removeAllViews(); adchoiceViewLayout.setVisibility(View.GONE); } nativeAdLayout.addView(nativecustomLayout); nativeAdLayout.setVisibility(View.VISIBLE); nativeAd.registerViewForInteraction(adView, nativecustomLayout, nativecustomLayout, listview); } } @Override public void didInteractWithAd(VmaxAdView adView) { } @Override public void willDismissAd(VmaxAdView adView) { } @Override public void willPresentAd(VmaxAdView adView) { } @Override public void willLeaveApp(VmaxAdView adView) { } @Override public void onVideoView(boolean b, int i, int i1) { } @Override public void onAdExpand() { } @Override public void onAdCollapsed() { } }); vmaxAdView.cacheAd(); |
1 |
vmaxNativeAd.registerViewForInteraction(VmaxAdView adView, ViewGroup vgroup, View view, List listview); |
adView is mandatory for proper handling of Native Ads.
vgroup is main Ad parnetView and it is mandatory.
view is mandatory so as to set listener, it should be CTA view or any other Native Ad element view.
listview is Optional if click on multiple views required. If listview is provided than third argument View view should be parent of the view available in list.
Note: For AdMob AdChoice View icon, pass second parameter in above function that is Ad container Layout and keep top right space empty say 15px. VMAX SDK will handle to render AdMob Adchoice icon for AdMob.
Make sure to handle Activity life cycle callbacks with VmaxAdview for proper functioning of Ads
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
@Override protected void onPause() { if (vmaxAdView != null) { vmaxAdView.onPause(); } super.onPause(); } @Override protected void onResume() { if (vmaxAdView != null) { vmaxAdView.onResume(); } super.onResume(); } @Override protected void onDestroy() { if (vmaxAdView != null) { vmaxAdView.onDestroy(); } super.onDestroy(); } @Override public void finish() { if (vmaxAdView != null) { vmaxAdView.finish(); } super.finish(); } @Override public void onBackPressed() { if (vmaxAdView != null) { vmaxAdView.onBackPressed(); } super.onBackPressed(); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); if (vmaxAdView != null) { vmaxAdView.onConfigurationChanged(); } } |