private NativeAd ad;
private void initAd()
{
// Enabling debug mode
// NativeAd.setDebugMode(true);
// Create an instance of NativeAd
ad = new NativeAd(YOUR_SLOT_ID, this);
}
private NativeAd ad;
private void initAd()
{
// Create an instance of NativeAd
ad = new NativeAd(YOUR_SLOT_ID, this);
// Set the event listener
ad.setListener(new NativeAd.NativeAdListener()
{
@Override
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
}
@Override
public void onNoAd(String reason, NativeAd ad)
{
}
@Override
public void onClick(NativeAd ad)
{
}
@Override
public void onShow(NativeAd ad)
{
}
@Override
public void onVideoPlay(NativeAd ad)
{
}
@Override
public void onVideoPause(NativeAd ad)
{
}
@Override
public void onVideoComplete(NativeAd ad)
{
}
});
// Start loading ad
ad.load();
}
ad.setCachePolicy(CachePolicy.NONE);
ad.load();
@Override
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
// Ad title
String title = banner.getTitle();
// Main text
String description = banner.getDescription();
// Age limit. May be null
String ageRestrictions = banner.getAgeRestrictions();
// Disclaimer. May be null
String disclaimer = banner.getDisclaimer();
// "Advertising" label text
String advertisingLabel = banner.getAdvertisingLabel();
// Icon
ImageData icon = banner.getIcon();
// Call-to-action text for the button
String ctaText = banner.getCtaText();
// Properties available only for ads promoting apps
if (banner.getNavigationType().equals(NavigationType.STORE))
{
// App rating (0-5)
float rating = banner.getRating();
// Number of votes
int votes = banner.getVotes();
// App category
String category = banner.getCategory();
// App subcategory
String subcategory = banner.getSubcategory();
}
// Properties available only for ads promoting websites
else if (banner.getNavigationType().equals(NavigationType.WEB))
{
// Website domain
String domain = banner.getDomain();
}
// Use properties to build your visual component
Context context = YourActivity.this;
LinearLayout adViewLayout = new LinearLayout(context);
adViewLayout.setId(R.id.nativeads_ad_view);
TextView titleView = new TextView(context);
titleView.setId(R.id.nativeads_title);
titleView.setText(title);
adViewLayout.addView(titleView);
TextView descriptionView = new TextView(context);
descriptionView.setId(R.id.nativeads_description);
titleView.setText(description);
adViewLayout.addView(descriptionView);
Button btn = new Button(context);
btn.setId(R.id.nativeads_call_to_action);
btn.setText(ctaText);
adViewLayout.addView(btn);
// Create an instance of MediaAdView
MediaAdView mediaView = NativeViewsFactory.getMediaAdView(context);
mediaView.setId(R.id.nativeads_media_view);
// Create an instance of IconAdView
IconAdView iconView = new IconAdView(context);
mediaView.setId(R.id.nativeads_icon);
// If you have enabled autoloading of images, you can assign the loaded icon here: iconView.setImageBitmap(image.getBitmap());
adViewLayout.addView(mediaView);
adViewLayout.addView(iconView);
// Create container
NativeAdContainer nativeAdContainer = new NativeAdContainer(context);
// Add view to container
nativeAdContainer.addView(adViewLayout);
// Register the view
ad.registerView(adViewLayout);
// Add it to the layout
mainLayout.addView(nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
...
ad.setAdChoicesPlacement(AdChoicesPlacement.TOP_RIGHT);
ad.load();
@Override
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
...
...
...
// Create an array of clickable visual components, titleView and cta button
ArrayList<View> clickableViews = new ArrayList<>();
clickableViews.add(titleView);
clickableViews.add(btn);
// Register the view with clickable titleView and button
ad.registerView(adViewLayout, clickableViews);
// Add it to the layout
mainLayout.addView(adViewLayout, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
public void onLoad(NativePromoBanner banner, NativeAd ad)
{
// Create visual component
NativeAdView nativeAdView = NativeViewsFactory.getNativeAdView(ad, YourActivity.this);
// Available inner components
TextView advLabelView = nativeAdView.getAdvertisingTextView();
TextView ageRestrictionView = nativeAdView.getAgeRestrictionTextView();
TextView disclaimerView = nativeAdView.getDisclaimerTextView();
TextView titleView = nativeAdView.getTitleTextView();
TextView descriptionView = nativeAdView.getDescriptionTextView();
Button ctaBtn = nativeAdView.getCtaButtonView();
TextView votesView = nativeAdView.getVotesTextView();
StarsRatingView starsRatingView = nativeAdView.getStarsRatingView();
TextView domainView = nativeAdView.getDomainOrCategoryTextView();
MediaAdView mediaView = nativeAdView.getMediaAdView();
IconAdView iconView = nativeAdView.getIconImageView();
// Create container for creative
NativeAdContainer nativeAdContainer = new NativeAdContainer(context);
// Add ad creative to container
nativeAdContainer.addView(nativeAdView);
// Register visual component
ad.registerView(nativeAdContainer);
// Add to screen
mainLayout.addView(nativeAdContainer, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
}
// Enabling debug mode
// NativeAdLoader.setDebugMode(true);
// Create an instance of NativeAdLoader
NativeAdLoader nativeAdLoader = NativeAdLoader.newLoader(YOUR_SLOT_ID, COUNT, this);
// Set the OnLoad callback and loading ads
nativeAdLoader.setOnLoad(new OnLoad()
{
@Override
public void onLoad(@NonNull List<NativeAd> ads)
{
for (NativeAd ad : ads)
{
// Set the event listener
ad.setListener(nativeAdListener);
NativePromoBanner promoBanner = ad.getBanner();
// The same code as in onLoad method of NativeAdListener interface
}
}
}).load();