// In the Activity that will launch the native ad,// implement the AdListener interface and add the following:import com.facebook.ads.*;privateNativeAd nativeAd;privatevoid showNativeAd(){
nativeAd =newNativeAd(this,"841988475951113_841992455950715");
nativeAd.setAdListener(newAdListener(){@Overridepublicvoid onError(Ad ad,AdError error){...}@Overridepublicvoid onAdLoaded(Ad ad){...}@Overridepublicvoid onAdClicked(Ad ad){...}});
nativeAd.loadAd();}// The next step is to extract the ad metadata and use its properties // to build your customized native UI. Modify the onAdLoaded function // above to retrieve the ad properties. For example:@Overridepublicvoid onAdLoaded(Ad ad){if(ad != nativeAd){return;}String titleForAd = nativeAd.getAdTitle();Image coverImage = nativeAd.getAdCoverImage();Image iconForAd = nativeAd.getAdIcon();String socialContextForAd = nativeAd.getAdSocialContext();String titleForAdButton = nativeAd.getAdCallToAction();String textForAdBody = nativeAd.getAdBody();Rating appRatingForAd = nativeAd.getAdStarRating();// Add code here to create a custom view that uses the ad properties// For example:LinearLayout nativeAdContainer =newLinearLayout(this);TextView titleLabel =newTextView(this);
titleLabel.setText(titleForAd);
nativeAdContainer.addView(titleLabel);...// Add the ad to your layoutLinearLayout mainContainer =(LinearLayout)findViewById(R.id.MainContainer);
mainContainer.addView(nativeAdContainer);// Register the native ad view with the native ad instance
nativeAd.registerViewForInteraction(nativeAdContainer);}