Android studio facebook audience-network/android-native-ads
Using the Native Ad API in Android
View #1: Ad Icon
View #2: Ad Title
View #3: Sponsored Label
View #4: AdOptionsView
View #5: MediaView
View #6: Social Context
View #7: Ad Body
View #8: Call to Action button
Native Ad Steps
Step 1: Requesting a Native Ad
Step 2: Creating your Native Ad Layout
Step 3: Populating your Layout Using the Ad's Metadata
Step 4: Using MediaView
Setp 5: Load Ad without Auto Cache
Initialize the Audience Network SDK
Step 1: Requesting a Native Ad
Step 2: Creating your Native Ad Layout
Step 3: Populating your Layout Using the Ad's Metadata
Scenario 1: Immediately display the ad once it is loaded successfully. Modify the onAdLoaded()
method above to retrieve the Native Ad's
properties and display it as follows:
private NativeAdLayout nativeAdLayout;
private LinearLayout adView;
private NativeAd nativeAd;
private void loadNativeAd() {
// Instantiate a NativeAd object.
// NOTE: the placement ID will eventually identify this as your App, you can ignore it for
// now, while you are testing and replace it later when you have signed up.
// While you are using this temporary code you will only get test ads and if you release
// your code like this to the Google Play your users will not receive ads (you will get a no fill error).
nativeAd = new NativeAd(this, "YOUR_PLACEMENT_ID");
nativeAd.setAdListener(new NativeAdListener() {
...
@Override
public void onAdLoaded(Ad ad) {
// Race condition, load() called again before last ad was displayed
if (nativeAd == null || nativeAd != ad) {
return;
}
// Inflate Native Ad into Container
inflateAd(nativeAd);
}
...
});
// Request an ad
nativeAd.loadAd();
}
private void inflateAd(NativeAd nativeAd) {
nativeAd.unregisterView();
// Add the Ad view into the ad container.
nativeAdLayout = findViewById(R.id.native_ad_container);
LayoutInflater inflater = LayoutInflater.from(NativeAdActivity.this);
// Inflate the Ad view. The layout referenced should be the one you created in the last step.
adView = (LinearLayout) inflater.inflate(R.layout.native_ad_layout_1, nativeAdLayout, false);
nativeAdLayout.addView(adView);
// Add the AdOptionsView
LinearLayout adChoicesContainer = findViewById(R.id.ad_choices_container);
AdOptionsView adOptionsView = new AdOptionsView(NativeAdActivity.this, nativeAd, nativeAdLayout);
adChoicesContainer.removeAllViews();
adChoicesContainer.addView(adOptionsView, 0);
// Create native UI using the ad metadata.
AdIconView nativeAdIcon = adView.findViewById(R.id.native_ad_icon);
TextView nativeAdTitle = adView.findViewById(R.id.native_ad_title);
MediaView nativeAdMedia = adView.findViewById(R.id.native_ad_media);
TextView nativeAdSocialContext = adView.findViewById(R.id.native_ad_social_context);
TextView nativeAdBody = adView.findViewById(R.id.native_ad_body);
TextView sponsoredLabel = adView.findViewById(R.id.native_ad_sponsored_label);
Button nativeAdCallToAction = adView.findViewById(R.id.native_ad_call_to_action);
// Set the Text.
nativeAdTitle.setText(nativeAd.getAdvertiserName());
nativeAdBody.setText(nativeAd.getAdBodyText());
nativeAdSocialContext.setText(nativeAd.getAdSocialContext());
nativeAdCallToAction.setVisibility(nativeAd.hasCallToAction() ? View.VISIBLE : View.INVISIBLE);
nativeAdCallToAction.setText(nativeAd.getAdCallToAction());
sponsoredLabel.setText(nativeAd.getSponsoredTranslation());
// Create a list of clickable views
List<View> clickableViews = new ArrayList<>();
clickableViews.add(nativeAdTitle);
clickableViews.add(nativeAdCallToAction);
// Register the Title and CTA button to listen for clicks.
nativeAd.registerViewForInteraction(
adView,
nativeAdMedia,
nativeAdIcon,
clickableViews);
}
Scenario 2: Display the ad in a few seconds or minutes after it is successfully loaded. You should check whether the ad has been invalidated before displaying it.
Controlling Clickable Area
Step 4: Using MediaView
Step 5: Load Ad without Auto Cache
Hardware Acceleration for Video Ads
Application Level
Activity Level
Next Steps
Android studio facebook audience-network/android-native-ads
Reviewed by Code Labe
on
April 12, 2019
Rating: