-
Notifications
You must be signed in to change notification settings - Fork 4
Native Integration
Native ads let you easily monetize your app in a way that’s consistent with its existing design.
Before integrating banner ads in your app, you’ll need to go through the steps in our Getting Started Guide to integrate the SDK into your project. #Sample code And obtain Native Ads Steps
obtain sample code:
For a complete example, check out the CMAdSDK-sample.
obtain Native Ads Steps:
1.Create NativeAdManager and set posid or INativeAdLoaderListener
2.Load Native Ad
3.Custom a layout for display Native Ad
4.Add the NativeAd View to the nativeAdContainer and display Native Ad
#Load Native ads in your app ###Check your AndroidManifest.xml file Be sure you have the following permissions:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />**Note:**The INTERNET permission is required which is used to access the internet to get ad. The ACCESS_NETWORK_STATE or ACCESS_WIFI_STATE permission are optional which used for checking whether there is avaible Internet connection(suggest configure them).
if you integrated PEGASI SDK Ext Version , Please declare the following activities:
<!--for Mopub -->
<activity android:name="com.mopub.mobileads.MoPubActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.mopub.mobileads.MraidActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.mopub.common.MoPubBrowser"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<activity android:name="com.mopub.mobileads.MraidVideoPlayerActivity"
android:configChanges="keyboardHidden|orientation|screenSize"/>
<!--for Mopub-->Add this tag to your to use Google Play Services:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />Important Note: If compiling against API Level below 13, exclude screenSize from the manifest entries. ###1.Create NativeAdManager and set posid or INativeAdLoaderListener ####1.1 SDK Base Version
//setp1 : create nativeAdManagerMini
//The first parameter:Context
//The second parameter: posid
nativeAdManagerMini = new NativeAdManagerMini(this, mAdPosid);
//setp2 : set callback listener(INativeAdLoaderListener)
nativeAdManagerMini.setNativeAdListener(new INativeAdLoaderListener() {
@Override
public void adLoaded() {
//load success
}
@Override
public void adFailedToLoad(int i) {
//load failed
}
@Override
public void adClicked(INativeAd iNativeAd) {
//ad click
}
});####1.2 SDK Ext Version
//setp1 : create nativeAdManagerEx
//The first parameter:Context
//The second parameter: posid
nativeAdManagerEx = new NativeAdManagerEx(this, mAdPosid);
//setp2 : set callback listener(INativeAdLoaderListener)
nativeAdManagerEx.setNativeAdListener(new INativeAdLoaderListener() {
@Override
public void adLoaded() {
//ad load success ,you can do other something here
}
@Override
public void adFailedToLoad(int i) {
//load failed
}
@Override
public void adClicked(INativeAd iNativeAd) {
//ad clicked
}
});###2.Load Native Ad ####2.1 SDK Base Version
nativeAdManagerMini.loadAd();####2.2 SDK Ext Version
nativeAdManagerEx.loadAd();###3.Custom a layout for display Native Ad ####3.1 SDK Base Version custom a layout for Native ad ,see native_ad_layout.xml from CMAdSDK-base-sample for a full example.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/ad_border"
android:layout_height="wrap_content" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:padding="10dp" >
<ImageView
android:id="@+id/big_iv_icon"
...
/>
<TextView
android:id="@+id/big_main_title"
...
/>
<TextView
android:id="@+id/big_sub_title"
...
/>
<Button
android:id="@+id/big_btn_install"
...
/>
</RelativeLayout>
</LinearLayout>Init native ad data for layout,see OrionNativeAdview.java from CMAdSDK-base-sample for a full example.
//step1: Your Ad layout
mNativeAdView = View.inflate(mContext, R.layout.native_ad_layout, this);
String iconUrl = ad.getAdIconUrl();
ImageView iconImageView = (ImageView) mNativeAdView
.findViewById(R.id.big_iv_icon);
if (iconUrl != null) {
VolleyUtil.loadImage(iconImageView, iconUrl);
}
//get image url
String mainImageUrl = ad.getAdCoverImageUrl();
if (!TextUtils.isEmpty(mainImageUrl)) {
ImageView imageViewMain = (ImageView) mNativeAdView
.findViewById(R.id.iv_main);
imageViewMain.setVisibility(View.VISIBLE);
VolleyUtil.loadImage(imageViewMain, mainImageUrl);
}
//fill ad data
TextView titleTextView = (TextView) mNativeAdView.findViewById(R.id.big_main_title);
TextView subtitleTextView = (TextView) mNativeAdView.findViewById(R.id.big_sub_title);
Button bigButton = (Button) mNativeAdView.findViewById(R.id.big_btn_install);
TextView bodyTextView = (TextView) mNativeAdView.findViewById(R.id.text_body);
titleTextView.setText(ad.getAdTitle());
subtitleTextView.setText(ad.getAdSocialContext());
bigButton.setText(ad.getAdCallToAction());
bodyTextView.setText(ad.getAdBody());
if (mNativeAd != null) {
mNativeAd.unregisterView();
}
mNativeAd = ad;
// step2: register view for ad
mNativeAd.registerViewForInteraction(mNativeAdView);####3.2 SDK Ext Version The layout for Native Ad as same with SDK Base Version , you can see native_ad_layout.xml from CMAdSDK-Ext-sample for a full example. Importance Note: If integrated the Admob ,you will provide two other layout for the Admob Ads .you can see admob_native_ad_layout_install.xml and admob_native_ad_layout_context.xml from CMAdSDK-Ext-sample for a full example. For example : admob_native_ad_layout_install.xml
<com.google.android.gms.ads.formats.NativeAppInstallAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/ad_border"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/big_iv_icon"
...
/>
<TextView
android:id="@+id/big_main_title"
...
/>
<TextView
android:id="@+id/big_sub_title"
...
/>
<Button
android:id="@+id/big_btn_install"
...
/>
</LinearLayout>
</com.google.android.gms.ads.formats.NativeAppInstallAdView>admob_native_ad_layout_context.xml
<com.google.android.gms.ads.formats.NativeContentAdView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="@drawable/ad_border"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/big_iv_icon"
...
/>
<TextView
android:id="@+id/big_main_title"
...
/>
<TextView
android:id="@+id/big_sub_title"
...
/>
<Button
android:id="@+id/big_btn_install"
...
/>
</LinearLayout>
</com.google.android.gms.ads.formats.NativeContentAdView>In addition to ,you need to bind view(see demo) :
//step1: Your Ad layout
// if integrate Admob Ad , you need notice :
// Admob Ad layout need the root of the layout which provide by Admob Ad
// if isDownLoadApp is true(direct download app) , use Layout Resource :R.layout.admob_native_ad_layout_install
if (ad.getAdTypeName().equals(Const.KEY_AB) && ad.isDownLoadApp()) {
mNativeAdView = View.inflate(mContext, R.layout.admob_native_ad_layout_install, this);
mNativeAdView = mNativeAdView.findViewById(R.id.admob_native_install_adview);
setAdmobInstallAdView((NativeAppInstallAdView) mNativeAdView);
}
else if(ad.getAdTypeName().equals(Const.KEY_AB) && !ad.isDownLoadApp()){
// if isDownLoadApp is false (display by webview or other way) ,
// use Layout Resource : R.layout.admob_native_ad_layout_context
mNativeAdView = View.inflate(mContext, R.layout.admob_native_ad_layout_context, this);
mNativeAdView = mNativeAdView.findViewById(R.id.admob_native_content_adview);
setAdmobContentAdView((NativeContentAdView) mNativeAdView);
}
else if(ad.getAdTypeName().equalsIgnoreCase("mpbanner")) {
//if mpbanner ,direct get View and add to parent view
mNativeAdView = (View) ad.getAdObject();
addView(mNativeAdView);
} else {
//if Ad resource from cm,fb,mopub ...
// use Layout Resource : R.layout.native_ad_layout
mNativeAdView = View.inflate(mContext, R.layout.native_ad_layout, this);
String iconUrl = ad.getAdIconUrl();
ImageView iconImageView = (ImageView) mNativeAdView
.findViewById(R.id.big_iv_icon);
if (iconUrl != null) {
VolleyUtil.loadImage(iconImageView, iconUrl);
}
//get image url
String mainImageUrl = ad.getAdCoverImageUrl();
if (!TextUtils.isEmpty(mainImageUrl)) {
ImageView imageViewMain = (ImageView) mNativeAdView
.findViewById(R.id.iv_main);
imageViewMain.setVisibility(View.VISIBLE);
VolleyUtil.loadImage(imageViewMain, mainImageUrl);
}
Log.e("URL", mainImageUrl != null ? mainImageUrl : "mainImageUrl is null");
TextView titleTextView = (TextView) mNativeAdView.findViewById(R.id.big_main_title);
TextView subtitleTextView = (TextView) mNativeAdView.findViewById(R.id.big_sub_title);
Button bigButton = (Button) mNativeAdView.findViewById(R.id.big_btn_install);
TextView bodyTextView = (TextView) mNativeAdView.findViewById(R.id.text_body);
//fill ad data
titleTextView.setText(ad.getAdTitle());
subtitleTextView.setText(ad.getAdTypeName());
bigButton.setText(ad.getAdCallToAction());
bodyTextView.setText(ad.getAdBody());
}
if (mNativeAd != null) {
mNativeAd.unregisterView();
}
mNativeAd = ad;
// step2: register view for ad
mNativeAd.registerViewForInteraction(mNativeAdView);Note: setAdmobInstallAdView() and setAdmobContentAdView() are used to binding view for Admob Layout . At the same time ,you must resiter the layout for ad.
//register view for ad
mNativeAd.registerViewForInteraction(mNativeAdView);Add the NativeAd View to the nativeAdContainer and display Native Ad
if(nativeAdManagerEx != null){
INativeAd ad = nativeAdManagerEx.getAd();
if (ad == null) {
Toast.makeText(NativeAdSampleActivity.this,
"no native ad loaded!", Toast.LENGTH_SHORT).show();
return;
}
if (mAdView != null) {
// remove old ad view
nativeAdContainer.removeView(mAdView);
}
//the mAdView is custom by publisher
mAdView = OrionNativeAdview.createAdView(getApplicationContext(), ad);
//add the mAdView into the layout of view container.
nativeAdContainer.addView(mAdView);
}