use of com.mopub.nativeads.MoPubNative in project mopub-android-mediation by mopub.
the class NativeManualFragment method onCreateView.
@Override
public View onCreateView(final LayoutInflater inflater, @Nullable final ViewGroup container, @Nullable final Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
final View view = inflater.inflate(R.layout.native_manual_fragment, container, false);
final DetailFragmentViewHolder viewHolder = DetailFragmentViewHolder.fromView(view);
mAdConfiguration = MoPubSampleAdUnit.fromBundle(getArguments());
mAdContainer = view.findViewById(R.id.parent_view);
viewHolder.mLoadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(final View v) {
updateRequestParameters(viewHolder);
if (mAdContainer != null) {
mAdContainer.removeAllViews();
}
if (mMoPubNative != null) {
mMoPubNative.makeRequest(mRequestParameters);
} else {
logToast(getActivity(), getName() + " failed to load. MoPubNative instance is null.");
}
}
});
String adUnitId = null;
if (mAdConfiguration != null) {
adUnitId = mAdConfiguration.getAdUnitId();
}
viewHolder.mDescriptionView.setText(mAdConfiguration.getDescription());
viewHolder.mAdUnitIdView.setText(adUnitId);
viewHolder.mKeywordsField.setText(getArguments().getString(MoPubListFragment.KEYWORDS_KEY, ""));
viewHolder.mUserDataKeywordsField.setText(getArguments().getString(MoPubListFragment.USER_DATA_KEYWORDS_KEY, ""));
updateRequestParameters(viewHolder);
mMoPubNative = new MoPubNative(getContext(), adUnitId, new MoPubNative.MoPubNativeNetworkListener() {
@Override
public void onNativeLoad(NativeAd nativeAd) {
NativeAd.MoPubNativeEventListener moPubNativeEventListener = new NativeAd.MoPubNativeEventListener() {
@Override
public void onImpression(View view) {
// The ad has registered an impression. You may call any app logic that
// depends on having the ad view shown.
logToast(getActivity(), getName() + " impressed.");
}
@Override
public void onClick(View view) {
logToast(getActivity(), getName() + " clicked.");
}
};
// In a manual integration, any interval that is at least 2 is acceptable
final AdapterHelper adapterHelper = new AdapterHelper(getContext(), 0, 2);
final View adView;
adView = adapterHelper.getAdView(null, null, nativeAd, new ViewBinder.Builder(0).build());
nativeAd.setMoPubNativeEventListener(moPubNativeEventListener);
if (mAdContainer != null) {
mAdContainer.addView(adView);
} else {
logToast(getActivity(), getName() + " failed to show. Ad container is null.");
}
}
@Override
public void onNativeFail(NativeErrorCode errorCode) {
logToast(getActivity(), getName() + " failed to load: " + errorCode.toString());
}
});
MoPubStaticNativeAdRenderer moPubStaticNativeAdRenderer = new MoPubStaticNativeAdRenderer(new ViewBinder.Builder(R.layout.native_ad_list_item).titleId(R.id.native_title).textId(R.id.native_text).mainImageId(R.id.native_main_image).iconImageId(R.id.native_icon_image).callToActionId(R.id.native_cta).privacyInformationIconImageId(R.id.native_privacy_information_icon_image).sponsoredTextId(R.id.native_sponsored_text_view).build());
// Set up a renderer for Facebook ads.
final FacebookAdRenderer facebookAdRenderer = new FacebookAdRenderer(new FacebookAdRenderer.FacebookViewBinder.Builder(R.layout.native_ad_fan_list_item).titleId(R.id.native_title).textId(R.id.native_text).mediaViewId(R.id.native_media_view).adIconViewId(R.id.native_icon).callToActionId(R.id.native_cta).adChoicesRelativeLayoutId(R.id.native_privacy_information_icon_layout).build());
// Set up a renderer for AdMob ads.
final GooglePlayServicesAdRenderer googlePlayServicesAdRenderer = new GooglePlayServicesAdRenderer(new GooglePlayServicesViewBinder.Builder(R.layout.video_ad_list_item).titleId(R.id.native_title).textId(R.id.native_text).mediaLayoutId(R.id.native_media_layout).iconImageId(R.id.native_icon_image).callToActionId(R.id.native_cta).privacyInformationIconImageId(R.id.native_privacy_information_icon_image).build());
// Set up a renderer for Verizon ads.
final VerizonNativeAdRenderer verizonNativeAdRenderer = new VerizonNativeAdRenderer(new ViewBinder.Builder(R.layout.native_ad_list_item).titleId(R.id.native_title).textId(R.id.native_text).mainImageId(R.id.native_main_image).iconImageId(R.id.native_icon_image).callToActionId(R.id.native_cta).privacyInformationIconImageId(R.id.native_privacy_information_icon_image).build());
// Set up a renderer for Pangle ads.
final PangleAdRenderer pangleAdRenderer = new PangleAdRenderer(new PangleAdViewBinder.Builder(R.layout.native_ad_pangle_list_item).callToActionId(R.id.native_cta).decriptionTextId(R.id.native_text).iconImageId(R.id.native_icon_image).titleId(R.id.native_title).mediaViewIdId(R.id.native_main_image).build());
// Set up a renderer for the reference network
final ReferenceNativeAdRenderer referenceNativeAdRenderer = new ReferenceNativeAdRenderer(new ReferenceNativeAdRenderer.ReferenceViewBinder.Builder(R.layout.native_ad_list_item).titleId(R.id.native_title).textId(R.id.native_text).mainImageId(R.id.native_main_image).iconImageId(R.id.native_icon_image).callToActionId(R.id.native_cta).adChoicesRelativeLayoutId(R.id.native_privacy_information_icon_layout).build());
// Set up a renderer for Mintegral ads
final MintegralAdRenderer mintegralAdRenderer = new MintegralAdRenderer(new MintegralAdRenderer.ViewBinder.Builder(R.layout.native_ad_list_item).titleId(R.id.native_title).textId(R.id.native_text).mainImageId(R.id.native_main_image).iconImageId(R.id.native_icon_image).callToActionId(R.id.native_cta).build());
// The first renderer that can handle a particular native ad gets used.
// We are prioritizing network renderers.
mMoPubNative.registerAdRenderer(mintegralAdRenderer);
mMoPubNative.registerAdRenderer(facebookAdRenderer);
mMoPubNative.registerAdRenderer(googlePlayServicesAdRenderer);
mMoPubNative.registerAdRenderer(verizonNativeAdRenderer);
mMoPubNative.registerAdRenderer(pangleAdRenderer);
mMoPubNative.registerAdRenderer(referenceNativeAdRenderer);
mMoPubNative.registerAdRenderer(moPubStaticNativeAdRenderer);
mMoPubNative.makeRequest(mRequestParameters);
return view;
}
use of com.mopub.nativeads.MoPubNative in project prebid-mobile-android by prebid.
the class XandrNativeInAppMoPubDemoActivity method loadInAppNativeMopub.
// Mopub
private void loadInAppNativeMopub() {
{
removePreviousAds();
PrebidMobile.setPrebidServerHost(Host.APPNEXUS);
PrebidMobile.setPrebidServerAccountId(Constants.PBS_ACCOUNT_ID_APPNEXUS);
final NativeAdUnit nativeAdUnit = new NativeAdUnit(Constants.PBS_CONFIG_ID_NATIVE_APPNEXUS);
nativeAdUnit.setContextType(NativeAdUnit.CONTEXT_TYPE.SOCIAL_CENTRIC);
nativeAdUnit.setPlacementType(NativeAdUnit.PLACEMENTTYPE.CONTENT_FEED);
nativeAdUnit.setContextSubType(NativeAdUnit.CONTEXTSUBTYPE.GENERAL_SOCIAL);
adUnit = nativeAdUnit;
ArrayList<NativeEventTracker.EVENT_TRACKING_METHOD> methods = new ArrayList<>();
methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.IMAGE);
methods.add(NativeEventTracker.EVENT_TRACKING_METHOD.JS);
try {
NativeEventTracker tracker = new NativeEventTracker(NativeEventTracker.EVENT_TYPE.IMPRESSION, methods);
nativeAdUnit.addEventTracker(tracker);
} catch (Exception e) {
e.printStackTrace();
}
NativeTitleAsset title = new NativeTitleAsset();
title.setLength(90);
title.setRequired(true);
nativeAdUnit.addAsset(title);
NativeImageAsset icon = new NativeImageAsset();
icon.setImageType(NativeImageAsset.IMAGE_TYPE.ICON);
icon.setWMin(20);
icon.setHMin(20);
icon.setRequired(true);
nativeAdUnit.addAsset(icon);
NativeImageAsset image = new NativeImageAsset();
image.setImageType(NativeImageAsset.IMAGE_TYPE.MAIN);
image.setHMin(200);
image.setWMin(200);
image.setRequired(true);
nativeAdUnit.addAsset(image);
NativeDataAsset data = new NativeDataAsset();
data.setLen(90);
data.setDataType(NativeDataAsset.DATA_TYPE.SPONSORED);
data.setRequired(true);
nativeAdUnit.addAsset(data);
NativeDataAsset body = new NativeDataAsset();
body.setRequired(true);
body.setDataType(NativeDataAsset.DATA_TYPE.DESC);
nativeAdUnit.addAsset(body);
NativeDataAsset cta = new NativeDataAsset();
cta.setRequired(true);
cta.setDataType(NativeDataAsset.DATA_TYPE.CTATEXT);
nativeAdUnit.addAsset(cta);
moPubNative = new MoPubNative(XandrNativeInAppMoPubDemoActivity.this, "2674981035164b2db5ef4b4546bf3d49", new MoPubNative.MoPubNativeNetworkListener() {
@Override
public void onNativeLoad(final NativeAd nativeAd) {
Log.d("Prebid", "MoPub native ad loaded");
XandrNativeInAppMoPubDemoActivity.this.ad = nativeAd;
AdViewUtils.findNative(nativeAd, new PrebidNativeAdListener() {
@Override
public void onPrebidNativeLoaded(final PrebidNativeAd ad) {
inflatePrebidNativeAd(ad);
}
@Override
public void onPrebidNativeNotFound() {
inflateMoPubNativeAd(nativeAd);
}
@Override
public void onPrebidNativeNotValid() {
Log.e("ERROR", "onPrebidNativeNotValid");
// should not show the NativeAd on the screen, do something else
}
});
}
@Override
public void onNativeFail(NativeErrorCode errorCode) {
Log.d("Prebid", "MoPub native failed to load: " + errorCode.toString());
}
});
moPubNative.registerAdRenderer(new MoPubStaticNativeAdRenderer(null));
RequestParameters.Builder mRP = new RequestParameters.Builder();
// Fetching the demannd using OnCompleteListener
nativeAdUnit.fetchDemand(mRP, new OnCompleteListener() {
@Override
public void onComplete(ResultCode resultCode) {
if (resultCode == ResultCode.SUCCESS) {
moPubNative.makeRequest(mRP.build());
} else {
Toast.makeText(XandrNativeInAppMoPubDemoActivity.this, "Native Ad Unit: " + resultCode.name(), Toast.LENGTH_SHORT).show();
}
refreshCount++;
XandrNativeInAppMoPubDemoActivity.this.resultCode = resultCode;
request = mRP.build();
}
});
// ================================================================================
// SAMPLE CODE: Fetching the demand using OnCompleteListener2
// ================================================================================
/*
nativeAdUnit.fetchDemand(new OnCompleteListener2() {
@Override
public void onComplete(ResultCode resultCode, Map<String, String> unmodifiableMap) {
Log.e("MAP", unmodifiableMap.toString());
if (resultCode == ResultCode.SUCCESS) {
String keywords = "";
for (String key: unmodifiableMap.keySet()) {
keywords += key + ":" + unmodifiableMap.get(key) + ",";
}
// removing last ","
keywords = keywords.substring(0, keywords.length()-1);
RequestParameters mRP = new RequestParameters.Builder().keywords(keywords).build();
Log.d("Prebid", mRP.getKeywords());
mMoPubNative.makeRequest(mRP);
}
Toast.makeText(XandrNativeInAppMoPubDemoActivity.this, "Native Ad Unit: " + resultCode.name(), Toast.LENGTH_SHORT).show();
}
});
*/
// ================================================================================
// SAMPLE CODE: END
// ================================================================================
}
}
Aggregations